<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>AJ</title>
    <meta name="generator" content="VuePress 1.5.0">
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
    <script>
            var _hmt = _hmt || [];
            (function() {
            var hm = document.createElement("script");
            hm.src = "https://hm.baidu.com/hm.js?51b8c2e72d1adf96524638ce85bb7d72";
            var s = document.getElementsByTagName("script")[0]; 
            s.parentNode.insertBefore(hm, s);

            // 引入谷歌,不需要可删除这段
            var hm1 = document.createElement("script");
            hm1.src = "https://www.googletagmanager.com/gtag/js?id=G-B7351EYS04";
            var s1 = document.getElementsByTagName("script")[0]; 
            s1.parentNode.insertBefore(hm1, s1);
            })();

            // 谷歌加载,不需要可删除
            window.dataLayer = window.dataLayer || [];
            function gtag(){dataLayer.push(arguments);}
            gtag('js', new Date());

            gtag('config', 'G-B7351EYS04');
        </script>
    <meta name="description" content="阿俊博客|记录一个程序员的成长历程 阿俊博客(blog.zhequtao.com)记录一个后端程序员的成长历程,分享他的学习笔记和心得,还有些开发必备技巧哦! java golang web html dev开发">
    <link rel="preload" href="/assets/css/0.styles.a3df589e.css" as="style"><link rel="preload" href="/assets/js/app.cb35c8f6.js" as="script"><link rel="preload" href="/assets/js/2.063846a6.js" as="script"><link rel="preload" href="/assets/js/4.1ffb4609.js" as="script"><link rel="preload" href="/assets/js/45.f8a2a3b3.js" as="script"><link rel="prefetch" href="/assets/js/10.5629fe4d.js"><link rel="prefetch" href="/assets/js/100.8417ccea.js"><link rel="prefetch" href="/assets/js/101.37fdb377.js"><link rel="prefetch" href="/assets/js/102.848b686d.js"><link rel="prefetch" href="/assets/js/103.6a2489a0.js"><link rel="prefetch" href="/assets/js/104.99e8899f.js"><link rel="prefetch" href="/assets/js/105.8b741763.js"><link rel="prefetch" href="/assets/js/106.08163715.js"><link rel="prefetch" href="/assets/js/107.21801349.js"><link rel="prefetch" href="/assets/js/108.e3a2892b.js"><link rel="prefetch" href="/assets/js/109.dab5618c.js"><link rel="prefetch" href="/assets/js/11.cddc1623.js"><link rel="prefetch" href="/assets/js/110.f5249b39.js"><link rel="prefetch" href="/assets/js/111.9abf8bce.js"><link rel="prefetch" href="/assets/js/112.8eb86924.js"><link rel="prefetch" href="/assets/js/113.c180ed46.js"><link rel="prefetch" href="/assets/js/114.2370f5b5.js"><link rel="prefetch" href="/assets/js/115.88dc3fd8.js"><link rel="prefetch" href="/assets/js/116.63403600.js"><link rel="prefetch" href="/assets/js/117.83674aa0.js"><link rel="prefetch" href="/assets/js/118.9c58c685.js"><link rel="prefetch" href="/assets/js/119.621f9a70.js"><link rel="prefetch" href="/assets/js/12.ff3d03f5.js"><link rel="prefetch" href="/assets/js/120.a6d2b7f7.js"><link rel="prefetch" href="/assets/js/121.ae0ce9d1.js"><link rel="prefetch" href="/assets/js/122.de240e6d.js"><link rel="prefetch" href="/assets/js/123.b043e3e4.js"><link rel="prefetch" href="/assets/js/124.550af937.js"><link rel="prefetch" href="/assets/js/125.6f06b34c.js"><link rel="prefetch" href="/assets/js/126.38d64604.js"><link rel="prefetch" href="/assets/js/127.a948db56.js"><link rel="prefetch" href="/assets/js/128.4ca789e0.js"><link rel="prefetch" href="/assets/js/129.59be4505.js"><link rel="prefetch" href="/assets/js/13.e9f75cc3.js"><link rel="prefetch" href="/assets/js/130.ddae76a9.js"><link rel="prefetch" href="/assets/js/131.d969c265.js"><link rel="prefetch" href="/assets/js/132.5f43ce5c.js"><link rel="prefetch" href="/assets/js/133.b651af22.js"><link rel="prefetch" href="/assets/js/134.2499236a.js"><link rel="prefetch" href="/assets/js/135.4180596c.js"><link rel="prefetch" href="/assets/js/136.2c309233.js"><link rel="prefetch" href="/assets/js/137.72ca42dd.js"><link rel="prefetch" href="/assets/js/138.3772cd38.js"><link rel="prefetch" href="/assets/js/139.1d0a53da.js"><link rel="prefetch" href="/assets/js/14.be4b89ed.js"><link rel="prefetch" href="/assets/js/140.22368353.js"><link rel="prefetch" href="/assets/js/141.a14aef3c.js"><link rel="prefetch" href="/assets/js/142.2041ce3a.js"><link rel="prefetch" href="/assets/js/143.377262ec.js"><link rel="prefetch" href="/assets/js/144.8fcbc368.js"><link rel="prefetch" href="/assets/js/145.46da36bf.js"><link rel="prefetch" href="/assets/js/146.eceae0d9.js"><link rel="prefetch" href="/assets/js/147.e1bd6531.js"><link rel="prefetch" href="/assets/js/148.76f193ce.js"><link rel="prefetch" href="/assets/js/149.587bd581.js"><link rel="prefetch" href="/assets/js/15.e3219ac0.js"><link rel="prefetch" href="/assets/js/150.169c71e0.js"><link rel="prefetch" href="/assets/js/151.384021ea.js"><link rel="prefetch" href="/assets/js/152.0f20cf03.js"><link rel="prefetch" href="/assets/js/153.fd94af1e.js"><link rel="prefetch" href="/assets/js/154.a550f5dc.js"><link rel="prefetch" href="/assets/js/155.ba1ae86e.js"><link rel="prefetch" href="/assets/js/156.3b98ded0.js"><link rel="prefetch" href="/assets/js/157.cd378596.js"><link rel="prefetch" href="/assets/js/158.c138d0df.js"><link rel="prefetch" href="/assets/js/159.2635f7f4.js"><link rel="prefetch" href="/assets/js/16.c9c35d42.js"><link rel="prefetch" href="/assets/js/160.d51b4126.js"><link rel="prefetch" href="/assets/js/161.5dc29e7b.js"><link rel="prefetch" href="/assets/js/162.f257a92b.js"><link rel="prefetch" href="/assets/js/163.9eb75e40.js"><link rel="prefetch" href="/assets/js/164.8fb1e22b.js"><link rel="prefetch" href="/assets/js/165.49255503.js"><link rel="prefetch" href="/assets/js/166.e54539e6.js"><link rel="prefetch" href="/assets/js/167.a06acdb0.js"><link rel="prefetch" href="/assets/js/168.948ab620.js"><link rel="prefetch" href="/assets/js/169.7d756812.js"><link rel="prefetch" href="/assets/js/17.81067bc8.js"><link rel="prefetch" href="/assets/js/170.ffe63330.js"><link rel="prefetch" href="/assets/js/171.835398b9.js"><link rel="prefetch" href="/assets/js/172.3987be39.js"><link rel="prefetch" href="/assets/js/173.e3cedb8a.js"><link rel="prefetch" href="/assets/js/174.11a32588.js"><link rel="prefetch" href="/assets/js/175.da6f9782.js"><link rel="prefetch" href="/assets/js/176.50e55edc.js"><link rel="prefetch" href="/assets/js/177.a89a1d17.js"><link rel="prefetch" href="/assets/js/178.7ad3ce84.js"><link rel="prefetch" href="/assets/js/179.98a1343b.js"><link rel="prefetch" href="/assets/js/18.b67caf4c.js"><link rel="prefetch" href="/assets/js/180.4e98599b.js"><link rel="prefetch" href="/assets/js/181.dd885afd.js"><link rel="prefetch" href="/assets/js/182.789990c8.js"><link rel="prefetch" href="/assets/js/183.f11ca2fd.js"><link rel="prefetch" href="/assets/js/184.fcf128ec.js"><link rel="prefetch" href="/assets/js/185.54e1e9b6.js"><link rel="prefetch" href="/assets/js/186.db91021a.js"><link rel="prefetch" href="/assets/js/187.5309dd42.js"><link rel="prefetch" href="/assets/js/188.615821ea.js"><link rel="prefetch" href="/assets/js/189.f23f1d42.js"><link rel="prefetch" href="/assets/js/19.294a011c.js"><link rel="prefetch" href="/assets/js/190.efe894bc.js"><link rel="prefetch" href="/assets/js/191.0d14904a.js"><link rel="prefetch" href="/assets/js/192.efd25a9e.js"><link rel="prefetch" href="/assets/js/193.88151f34.js"><link rel="prefetch" href="/assets/js/194.efedce14.js"><link rel="prefetch" href="/assets/js/195.03ac15bb.js"><link rel="prefetch" href="/assets/js/196.fbc18389.js"><link rel="prefetch" href="/assets/js/197.0257ab5e.js"><link rel="prefetch" href="/assets/js/198.f1de2817.js"><link rel="prefetch" href="/assets/js/199.8f1c166b.js"><link rel="prefetch" href="/assets/js/20.1a41edb1.js"><link rel="prefetch" href="/assets/js/200.4af99727.js"><link rel="prefetch" href="/assets/js/201.9f8caef1.js"><link rel="prefetch" href="/assets/js/202.6f07d705.js"><link rel="prefetch" href="/assets/js/203.5012fe50.js"><link rel="prefetch" href="/assets/js/204.3a0dfd8e.js"><link rel="prefetch" href="/assets/js/205.0ba9b606.js"><link rel="prefetch" href="/assets/js/206.663a49ec.js"><link rel="prefetch" href="/assets/js/207.b2406149.js"><link rel="prefetch" href="/assets/js/208.b0b2dfd8.js"><link rel="prefetch" href="/assets/js/209.41a0ec9f.js"><link rel="prefetch" href="/assets/js/21.b6ea6f9a.js"><link rel="prefetch" href="/assets/js/210.6f36beb8.js"><link rel="prefetch" href="/assets/js/211.90ef6e5c.js"><link rel="prefetch" href="/assets/js/212.42339063.js"><link rel="prefetch" href="/assets/js/213.a2bfeda9.js"><link rel="prefetch" href="/assets/js/214.5c7eb42a.js"><link rel="prefetch" href="/assets/js/215.18260ae5.js"><link rel="prefetch" href="/assets/js/216.7678fd29.js"><link rel="prefetch" href="/assets/js/217.9d936f88.js"><link rel="prefetch" href="/assets/js/218.bac403c4.js"><link rel="prefetch" href="/assets/js/219.9d8cb16b.js"><link rel="prefetch" href="/assets/js/22.367cc253.js"><link rel="prefetch" href="/assets/js/220.375b7707.js"><link rel="prefetch" href="/assets/js/221.3e400da4.js"><link rel="prefetch" href="/assets/js/222.8fbd2857.js"><link rel="prefetch" href="/assets/js/223.76a10075.js"><link rel="prefetch" href="/assets/js/224.ff94cc4e.js"><link rel="prefetch" href="/assets/js/225.d4688ca6.js"><link rel="prefetch" href="/assets/js/226.b13b18af.js"><link rel="prefetch" href="/assets/js/227.288d0f96.js"><link rel="prefetch" href="/assets/js/228.60430ac0.js"><link rel="prefetch" href="/assets/js/229.da728342.js"><link rel="prefetch" href="/assets/js/23.bc3de730.js"><link rel="prefetch" href="/assets/js/230.d8417d20.js"><link rel="prefetch" href="/assets/js/231.6a9ce0b4.js"><link rel="prefetch" href="/assets/js/232.7611b413.js"><link rel="prefetch" href="/assets/js/233.94712cf9.js"><link rel="prefetch" href="/assets/js/234.93888298.js"><link rel="prefetch" href="/assets/js/235.62506981.js"><link rel="prefetch" href="/assets/js/236.5a055c7c.js"><link rel="prefetch" href="/assets/js/237.0a6c4902.js"><link rel="prefetch" href="/assets/js/238.e5f37663.js"><link rel="prefetch" href="/assets/js/239.331bba86.js"><link rel="prefetch" href="/assets/js/24.82f0901c.js"><link rel="prefetch" href="/assets/js/240.59591cfc.js"><link rel="prefetch" href="/assets/js/241.ce671f47.js"><link rel="prefetch" href="/assets/js/242.fb2542fa.js"><link rel="prefetch" href="/assets/js/243.74abc73d.js"><link rel="prefetch" href="/assets/js/244.ecd707b2.js"><link rel="prefetch" href="/assets/js/245.d728367b.js"><link rel="prefetch" href="/assets/js/246.9270e7fe.js"><link rel="prefetch" href="/assets/js/247.a421ba15.js"><link rel="prefetch" href="/assets/js/248.e759132e.js"><link rel="prefetch" href="/assets/js/249.3077fb46.js"><link rel="prefetch" href="/assets/js/25.95c551dc.js"><link rel="prefetch" href="/assets/js/250.5fe4dd03.js"><link rel="prefetch" href="/assets/js/251.4b8fe76c.js"><link rel="prefetch" href="/assets/js/252.0d1cf7ea.js"><link rel="prefetch" href="/assets/js/253.89d16ba0.js"><link rel="prefetch" href="/assets/js/254.8d1afc68.js"><link rel="prefetch" href="/assets/js/255.64e680cb.js"><link rel="prefetch" href="/assets/js/256.9defbd0e.js"><link rel="prefetch" href="/assets/js/257.1fef24fd.js"><link rel="prefetch" href="/assets/js/258.4e205286.js"><link rel="prefetch" href="/assets/js/259.f3d56efc.js"><link rel="prefetch" href="/assets/js/26.7fe56bac.js"><link rel="prefetch" href="/assets/js/260.378299e6.js"><link rel="prefetch" href="/assets/js/261.8e4fb397.js"><link rel="prefetch" href="/assets/js/262.54595d98.js"><link rel="prefetch" href="/assets/js/263.fb333dc1.js"><link rel="prefetch" href="/assets/js/264.13d13301.js"><link rel="prefetch" href="/assets/js/265.c85104bc.js"><link rel="prefetch" href="/assets/js/266.9f3d1f1c.js"><link rel="prefetch" href="/assets/js/267.fb392815.js"><link rel="prefetch" href="/assets/js/268.add7eacb.js"><link rel="prefetch" href="/assets/js/269.7e49fcc7.js"><link rel="prefetch" href="/assets/js/27.5547e5f0.js"><link rel="prefetch" href="/assets/js/270.22342070.js"><link rel="prefetch" href="/assets/js/271.59917b1a.js"><link rel="prefetch" href="/assets/js/272.4a4751d5.js"><link rel="prefetch" href="/assets/js/273.f4c2fa5c.js"><link rel="prefetch" href="/assets/js/274.1208bc94.js"><link rel="prefetch" href="/assets/js/275.f6528753.js"><link rel="prefetch" href="/assets/js/276.a68772cd.js"><link rel="prefetch" href="/assets/js/277.f154ab32.js"><link rel="prefetch" href="/assets/js/278.bb616a63.js"><link rel="prefetch" href="/assets/js/279.6a356cff.js"><link rel="prefetch" href="/assets/js/28.b08b2847.js"><link rel="prefetch" href="/assets/js/280.5afc20fc.js"><link rel="prefetch" href="/assets/js/281.3cd86225.js"><link rel="prefetch" href="/assets/js/282.3ecb92aa.js"><link rel="prefetch" href="/assets/js/283.85159ea7.js"><link rel="prefetch" href="/assets/js/284.b6545c68.js"><link rel="prefetch" href="/assets/js/285.5e83ee29.js"><link rel="prefetch" href="/assets/js/286.2aad298d.js"><link rel="prefetch" href="/assets/js/287.3ca76244.js"><link rel="prefetch" href="/assets/js/288.5b17963d.js"><link rel="prefetch" href="/assets/js/289.f47a759b.js"><link rel="prefetch" href="/assets/js/29.562dbf7d.js"><link rel="prefetch" href="/assets/js/290.36b603c7.js"><link rel="prefetch" href="/assets/js/291.a9bd8951.js"><link rel="prefetch" href="/assets/js/292.dfc54f28.js"><link rel="prefetch" href="/assets/js/293.2d061212.js"><link rel="prefetch" href="/assets/js/294.6d564a9e.js"><link rel="prefetch" href="/assets/js/295.a31dd593.js"><link rel="prefetch" href="/assets/js/296.3101e5e8.js"><link rel="prefetch" href="/assets/js/297.7efde936.js"><link rel="prefetch" href="/assets/js/298.8bb3aa68.js"><link rel="prefetch" href="/assets/js/299.8b51e6bd.js"><link rel="prefetch" href="/assets/js/3.76257eb7.js"><link rel="prefetch" href="/assets/js/30.221adea2.js"><link rel="prefetch" href="/assets/js/300.62effe45.js"><link rel="prefetch" href="/assets/js/301.98c863c5.js"><link rel="prefetch" href="/assets/js/302.e3493ab2.js"><link rel="prefetch" href="/assets/js/303.800c8028.js"><link rel="prefetch" href="/assets/js/304.b6f08986.js"><link rel="prefetch" href="/assets/js/305.09af356b.js"><link rel="prefetch" href="/assets/js/306.d0d3589d.js"><link rel="prefetch" href="/assets/js/307.ee2fd249.js"><link rel="prefetch" href="/assets/js/308.f3f76368.js"><link rel="prefetch" href="/assets/js/309.d2b5ce40.js"><link rel="prefetch" href="/assets/js/31.334fc8bb.js"><link rel="prefetch" href="/assets/js/310.b4fa2feb.js"><link rel="prefetch" href="/assets/js/311.7d747ef3.js"><link rel="prefetch" href="/assets/js/312.89bdff40.js"><link rel="prefetch" href="/assets/js/313.875b82fe.js"><link rel="prefetch" href="/assets/js/314.0bbe51c4.js"><link rel="prefetch" href="/assets/js/315.cc07dbcf.js"><link rel="prefetch" href="/assets/js/316.bc72b152.js"><link rel="prefetch" href="/assets/js/317.55462812.js"><link rel="prefetch" href="/assets/js/318.a158fda0.js"><link rel="prefetch" href="/assets/js/319.b03a5bd2.js"><link rel="prefetch" href="/assets/js/32.d6826b16.js"><link rel="prefetch" href="/assets/js/320.a5bd19b0.js"><link rel="prefetch" href="/assets/js/321.4f9faaa7.js"><link rel="prefetch" href="/assets/js/322.dbd3b4fa.js"><link rel="prefetch" href="/assets/js/323.a04e2062.js"><link rel="prefetch" href="/assets/js/324.c45b46cf.js"><link rel="prefetch" href="/assets/js/325.cd1460c4.js"><link rel="prefetch" href="/assets/js/326.bd90ef85.js"><link rel="prefetch" href="/assets/js/327.8bf38ef7.js"><link rel="prefetch" href="/assets/js/328.99e9aed3.js"><link rel="prefetch" href="/assets/js/329.de0012cb.js"><link rel="prefetch" href="/assets/js/33.b1059062.js"><link rel="prefetch" href="/assets/js/330.59f11391.js"><link rel="prefetch" href="/assets/js/331.6d16a13c.js"><link rel="prefetch" href="/assets/js/332.922ca235.js"><link rel="prefetch" href="/assets/js/333.c94c3602.js"><link rel="prefetch" href="/assets/js/334.77e02010.js"><link rel="prefetch" href="/assets/js/335.1e0c4f7b.js"><link rel="prefetch" href="/assets/js/336.5675dc4f.js"><link rel="prefetch" href="/assets/js/337.bb6e11dc.js"><link rel="prefetch" href="/assets/js/338.294981e9.js"><link rel="prefetch" href="/assets/js/339.d0376372.js"><link rel="prefetch" href="/assets/js/34.0c7d5782.js"><link rel="prefetch" href="/assets/js/340.64596428.js"><link rel="prefetch" href="/assets/js/341.ec0f9409.js"><link rel="prefetch" href="/assets/js/342.7abc47c4.js"><link rel="prefetch" href="/assets/js/343.4262d486.js"><link rel="prefetch" href="/assets/js/344.8729ad8c.js"><link rel="prefetch" href="/assets/js/345.c210d888.js"><link rel="prefetch" href="/assets/js/346.6f42f7cb.js"><link rel="prefetch" href="/assets/js/347.81b41ae5.js"><link rel="prefetch" href="/assets/js/348.07eca37c.js"><link rel="prefetch" href="/assets/js/349.8019d6f3.js"><link rel="prefetch" href="/assets/js/35.ae14e37f.js"><link rel="prefetch" href="/assets/js/350.57da2e7b.js"><link rel="prefetch" href="/assets/js/351.2e99afdf.js"><link rel="prefetch" href="/assets/js/352.67dd88b7.js"><link rel="prefetch" href="/assets/js/353.15b9f624.js"><link rel="prefetch" href="/assets/js/354.a63e8432.js"><link rel="prefetch" href="/assets/js/355.bbc16ee9.js"><link rel="prefetch" href="/assets/js/356.ff63d3bb.js"><link rel="prefetch" href="/assets/js/357.4fb2d941.js"><link rel="prefetch" href="/assets/js/358.55182977.js"><link rel="prefetch" href="/assets/js/359.265c3d26.js"><link rel="prefetch" href="/assets/js/36.b9ed4cf1.js"><link rel="prefetch" href="/assets/js/360.ced80eb3.js"><link rel="prefetch" href="/assets/js/361.afe6ba84.js"><link rel="prefetch" href="/assets/js/362.c2b62518.js"><link rel="prefetch" href="/assets/js/363.0c4a4800.js"><link rel="prefetch" href="/assets/js/364.ce60291b.js"><link rel="prefetch" href="/assets/js/365.0e3a61f9.js"><link rel="prefetch" href="/assets/js/366.d53ccb03.js"><link rel="prefetch" href="/assets/js/367.689e464d.js"><link rel="prefetch" href="/assets/js/368.418c571f.js"><link rel="prefetch" href="/assets/js/369.c2fff3c8.js"><link rel="prefetch" href="/assets/js/37.a021ac57.js"><link rel="prefetch" href="/assets/js/370.a932b958.js"><link rel="prefetch" href="/assets/js/371.7d153241.js"><link rel="prefetch" href="/assets/js/372.fb9878fa.js"><link rel="prefetch" href="/assets/js/373.85772e03.js"><link rel="prefetch" href="/assets/js/374.b4a8b1b6.js"><link rel="prefetch" href="/assets/js/375.32f70596.js"><link rel="prefetch" href="/assets/js/376.a16d79a8.js"><link rel="prefetch" href="/assets/js/377.c996b7e1.js"><link rel="prefetch" href="/assets/js/378.d37d15c7.js"><link rel="prefetch" href="/assets/js/379.b81ba7dd.js"><link rel="prefetch" href="/assets/js/38.89138658.js"><link rel="prefetch" href="/assets/js/380.524c9b31.js"><link rel="prefetch" href="/assets/js/381.7ebfd6db.js"><link rel="prefetch" href="/assets/js/382.29edda0f.js"><link rel="prefetch" href="/assets/js/383.9642a212.js"><link rel="prefetch" href="/assets/js/384.086e3b42.js"><link rel="prefetch" href="/assets/js/385.a9bb46a8.js"><link rel="prefetch" href="/assets/js/386.f2561a39.js"><link rel="prefetch" href="/assets/js/387.ba9b6aaa.js"><link rel="prefetch" href="/assets/js/388.e0ace495.js"><link rel="prefetch" href="/assets/js/389.ba8c09dd.js"><link rel="prefetch" href="/assets/js/39.04f331e3.js"><link rel="prefetch" href="/assets/js/390.de1bb48b.js"><link rel="prefetch" href="/assets/js/391.7cc6edeb.js"><link rel="prefetch" href="/assets/js/392.0493a6f7.js"><link rel="prefetch" href="/assets/js/393.ba2d3e62.js"><link rel="prefetch" href="/assets/js/394.b3aa9224.js"><link rel="prefetch" href="/assets/js/395.f4df3a60.js"><link rel="prefetch" href="/assets/js/396.09644790.js"><link rel="prefetch" href="/assets/js/397.76163964.js"><link rel="prefetch" href="/assets/js/398.377fd8fc.js"><link rel="prefetch" href="/assets/js/399.39059be5.js"><link rel="prefetch" href="/assets/js/40.a21abf05.js"><link rel="prefetch" href="/assets/js/400.559a547f.js"><link rel="prefetch" href="/assets/js/401.bc6d738c.js"><link rel="prefetch" href="/assets/js/402.c504ed7b.js"><link rel="prefetch" href="/assets/js/403.77a0af9c.js"><link rel="prefetch" href="/assets/js/404.0f408cc6.js"><link rel="prefetch" href="/assets/js/405.90b0bab6.js"><link rel="prefetch" href="/assets/js/406.01b11432.js"><link rel="prefetch" href="/assets/js/407.58991ce5.js"><link rel="prefetch" href="/assets/js/408.9806278e.js"><link rel="prefetch" href="/assets/js/409.8d9b4bb3.js"><link rel="prefetch" href="/assets/js/41.6bcfc592.js"><link rel="prefetch" href="/assets/js/410.02dee620.js"><link rel="prefetch" href="/assets/js/411.3d12d3a6.js"><link rel="prefetch" href="/assets/js/412.37a62624.js"><link rel="prefetch" href="/assets/js/413.bda7ca34.js"><link rel="prefetch" href="/assets/js/414.2abb6547.js"><link rel="prefetch" href="/assets/js/415.1d271923.js"><link rel="prefetch" href="/assets/js/416.d1b11dfe.js"><link rel="prefetch" href="/assets/js/417.fcbf07ff.js"><link rel="prefetch" href="/assets/js/418.524d40ba.js"><link rel="prefetch" href="/assets/js/419.f264e463.js"><link rel="prefetch" href="/assets/js/42.ec85a270.js"><link rel="prefetch" href="/assets/js/420.8b99e60d.js"><link rel="prefetch" href="/assets/js/421.c697d876.js"><link rel="prefetch" href="/assets/js/422.4af54ae0.js"><link rel="prefetch" href="/assets/js/423.b75f24ff.js"><link rel="prefetch" href="/assets/js/424.ea80054f.js"><link rel="prefetch" href="/assets/js/425.804b48b0.js"><link rel="prefetch" href="/assets/js/426.ffed8383.js"><link rel="prefetch" href="/assets/js/427.2040bd22.js"><link rel="prefetch" href="/assets/js/428.b878fb56.js"><link rel="prefetch" href="/assets/js/429.f81fd922.js"><link rel="prefetch" href="/assets/js/43.c174449d.js"><link rel="prefetch" href="/assets/js/430.16f328ab.js"><link rel="prefetch" href="/assets/js/431.56f11924.js"><link rel="prefetch" href="/assets/js/432.e4c77710.js"><link rel="prefetch" href="/assets/js/433.29acf14b.js"><link rel="prefetch" href="/assets/js/434.33ee22fc.js"><link rel="prefetch" href="/assets/js/435.516f7600.js"><link rel="prefetch" href="/assets/js/436.28ae526a.js"><link rel="prefetch" href="/assets/js/437.b9bac473.js"><link rel="prefetch" href="/assets/js/438.711bd934.js"><link rel="prefetch" href="/assets/js/439.fe5f28bd.js"><link rel="prefetch" href="/assets/js/44.8704338b.js"><link rel="prefetch" href="/assets/js/440.b2f48747.js"><link rel="prefetch" href="/assets/js/441.4862a724.js"><link rel="prefetch" href="/assets/js/442.751f8ae7.js"><link rel="prefetch" href="/assets/js/443.9998dbe9.js"><link rel="prefetch" href="/assets/js/444.23d3f688.js"><link rel="prefetch" href="/assets/js/445.72419f35.js"><link rel="prefetch" href="/assets/js/446.21e02b7b.js"><link rel="prefetch" href="/assets/js/447.6bd039e5.js"><link rel="prefetch" href="/assets/js/448.af29ff31.js"><link rel="prefetch" href="/assets/js/449.bbdca196.js"><link rel="prefetch" href="/assets/js/450.32e470b8.js"><link rel="prefetch" href="/assets/js/451.f4c8ff6a.js"><link rel="prefetch" href="/assets/js/452.c0ec8943.js"><link rel="prefetch" href="/assets/js/453.5af475c6.js"><link rel="prefetch" href="/assets/js/454.567aa6c1.js"><link rel="prefetch" href="/assets/js/455.326072d1.js"><link rel="prefetch" href="/assets/js/456.64b88847.js"><link rel="prefetch" href="/assets/js/457.61c91559.js"><link rel="prefetch" href="/assets/js/458.4f4d43d6.js"><link rel="prefetch" href="/assets/js/459.a88891cb.js"><link rel="prefetch" href="/assets/js/46.90855a0f.js"><link rel="prefetch" href="/assets/js/460.fb9e15f6.js"><link rel="prefetch" href="/assets/js/461.998a4991.js"><link rel="prefetch" href="/assets/js/462.56ecfb03.js"><link rel="prefetch" href="/assets/js/463.582cd053.js"><link rel="prefetch" href="/assets/js/464.f12a0050.js"><link rel="prefetch" href="/assets/js/465.cc3b08a8.js"><link rel="prefetch" href="/assets/js/466.ea7e4ce2.js"><link rel="prefetch" href="/assets/js/467.58700d93.js"><link rel="prefetch" href="/assets/js/468.bb9998cd.js"><link rel="prefetch" href="/assets/js/469.232d261d.js"><link rel="prefetch" href="/assets/js/47.02516d33.js"><link rel="prefetch" href="/assets/js/470.90ab2be8.js"><link rel="prefetch" href="/assets/js/471.3652fec6.js"><link rel="prefetch" href="/assets/js/472.bc7517c9.js"><link rel="prefetch" href="/assets/js/473.27b7892f.js"><link rel="prefetch" href="/assets/js/474.0d488768.js"><link rel="prefetch" href="/assets/js/475.bc33cf17.js"><link rel="prefetch" href="/assets/js/476.76ce67eb.js"><link rel="prefetch" href="/assets/js/477.a20657f6.js"><link rel="prefetch" href="/assets/js/478.8905ef26.js"><link rel="prefetch" href="/assets/js/479.23ec3dc9.js"><link rel="prefetch" href="/assets/js/48.8d38983c.js"><link rel="prefetch" href="/assets/js/480.105bf58e.js"><link rel="prefetch" href="/assets/js/481.d62a3699.js"><link rel="prefetch" href="/assets/js/482.6cb10cdb.js"><link rel="prefetch" href="/assets/js/483.15b9ef53.js"><link rel="prefetch" href="/assets/js/484.794aff9c.js"><link rel="prefetch" href="/assets/js/485.3e6ef3cb.js"><link rel="prefetch" href="/assets/js/486.fc20eb22.js"><link rel="prefetch" href="/assets/js/487.2a4f7d47.js"><link rel="prefetch" href="/assets/js/488.24ba9af5.js"><link rel="prefetch" href="/assets/js/489.d451928e.js"><link rel="prefetch" href="/assets/js/49.bd70c59c.js"><link rel="prefetch" href="/assets/js/490.d83e4fc7.js"><link rel="prefetch" href="/assets/js/491.0210a738.js"><link rel="prefetch" href="/assets/js/492.3a69cd85.js"><link rel="prefetch" href="/assets/js/493.89d2920f.js"><link rel="prefetch" href="/assets/js/494.b7d1c15e.js"><link rel="prefetch" href="/assets/js/495.7ccfe17f.js"><link rel="prefetch" href="/assets/js/496.0d6c912a.js"><link rel="prefetch" href="/assets/js/497.087ca2c6.js"><link rel="prefetch" href="/assets/js/498.f2b3c894.js"><link rel="prefetch" href="/assets/js/499.e741f312.js"><link rel="prefetch" href="/assets/js/5.405f2620.js"><link rel="prefetch" href="/assets/js/50.9df07553.js"><link rel="prefetch" href="/assets/js/500.cb9babb9.js"><link rel="prefetch" href="/assets/js/501.63a859b6.js"><link rel="prefetch" href="/assets/js/502.f346b273.js"><link rel="prefetch" href="/assets/js/503.cdcd3de3.js"><link rel="prefetch" href="/assets/js/504.e83ec450.js"><link rel="prefetch" href="/assets/js/505.95f18293.js"><link rel="prefetch" href="/assets/js/506.02060a3c.js"><link rel="prefetch" href="/assets/js/507.859d6ae4.js"><link rel="prefetch" href="/assets/js/508.9724d886.js"><link rel="prefetch" href="/assets/js/509.e17dd53e.js"><link rel="prefetch" href="/assets/js/51.3dfc6350.js"><link rel="prefetch" href="/assets/js/510.ea0c942f.js"><link rel="prefetch" href="/assets/js/511.64a7f9a8.js"><link rel="prefetch" href="/assets/js/512.6cce418a.js"><link rel="prefetch" href="/assets/js/513.ea813fe8.js"><link rel="prefetch" href="/assets/js/514.10242470.js"><link rel="prefetch" href="/assets/js/515.5308bdf6.js"><link rel="prefetch" href="/assets/js/516.dfb113bc.js"><link rel="prefetch" href="/assets/js/517.96c71069.js"><link rel="prefetch" href="/assets/js/518.5594480e.js"><link rel="prefetch" href="/assets/js/519.91e848ae.js"><link rel="prefetch" href="/assets/js/52.dd230cd7.js"><link rel="prefetch" href="/assets/js/520.ebe69ce9.js"><link rel="prefetch" href="/assets/js/521.92b80342.js"><link rel="prefetch" href="/assets/js/522.6ce581ea.js"><link rel="prefetch" href="/assets/js/523.40b7f2f9.js"><link rel="prefetch" href="/assets/js/524.4ef256d3.js"><link rel="prefetch" href="/assets/js/525.19741e4a.js"><link rel="prefetch" href="/assets/js/526.e5de1675.js"><link rel="prefetch" href="/assets/js/527.9a79cd42.js"><link rel="prefetch" href="/assets/js/528.72732eb8.js"><link rel="prefetch" href="/assets/js/529.2ea03e45.js"><link rel="prefetch" href="/assets/js/53.a349c565.js"><link rel="prefetch" href="/assets/js/530.5d1103e7.js"><link rel="prefetch" href="/assets/js/531.82b032d6.js"><link rel="prefetch" href="/assets/js/532.12ee4beb.js"><link rel="prefetch" href="/assets/js/533.db9a90f7.js"><link rel="prefetch" href="/assets/js/534.fe139db4.js"><link rel="prefetch" href="/assets/js/535.056a6fbb.js"><link rel="prefetch" href="/assets/js/536.6bf85c15.js"><link rel="prefetch" href="/assets/js/537.6e2e1ccf.js"><link rel="prefetch" href="/assets/js/538.54eddb3b.js"><link rel="prefetch" href="/assets/js/539.6f00a207.js"><link rel="prefetch" href="/assets/js/54.e292b52f.js"><link rel="prefetch" href="/assets/js/540.9c956205.js"><link rel="prefetch" href="/assets/js/541.50f41228.js"><link rel="prefetch" href="/assets/js/542.a2b1879e.js"><link rel="prefetch" href="/assets/js/543.4e5e17c9.js"><link rel="prefetch" href="/assets/js/544.63d85227.js"><link rel="prefetch" href="/assets/js/545.c3923644.js"><link rel="prefetch" href="/assets/js/546.e64ad073.js"><link rel="prefetch" href="/assets/js/547.35a8752d.js"><link rel="prefetch" href="/assets/js/548.b62a1348.js"><link rel="prefetch" href="/assets/js/549.369e2ea0.js"><link rel="prefetch" href="/assets/js/55.21bb2983.js"><link rel="prefetch" href="/assets/js/550.b4632248.js"><link rel="prefetch" href="/assets/js/551.18f1879c.js"><link rel="prefetch" href="/assets/js/552.162e63cd.js"><link rel="prefetch" href="/assets/js/553.6998130e.js"><link rel="prefetch" href="/assets/js/554.89126c3d.js"><link rel="prefetch" href="/assets/js/555.a5f63b8a.js"><link rel="prefetch" href="/assets/js/556.35381cef.js"><link rel="prefetch" href="/assets/js/557.ceeecf52.js"><link rel="prefetch" href="/assets/js/558.253e6e4f.js"><link rel="prefetch" href="/assets/js/559.5cfbb773.js"><link rel="prefetch" href="/assets/js/56.5382d6b4.js"><link rel="prefetch" href="/assets/js/560.cca52f22.js"><link rel="prefetch" href="/assets/js/561.ea8d1141.js"><link rel="prefetch" href="/assets/js/562.c32e7b96.js"><link rel="prefetch" href="/assets/js/563.9bdd42e7.js"><link rel="prefetch" href="/assets/js/564.b3b87ec0.js"><link rel="prefetch" href="/assets/js/565.4be0d0f7.js"><link rel="prefetch" href="/assets/js/566.9f379d12.js"><link rel="prefetch" href="/assets/js/567.261e3181.js"><link rel="prefetch" href="/assets/js/568.4229e365.js"><link rel="prefetch" href="/assets/js/569.e662c167.js"><link rel="prefetch" href="/assets/js/57.8129f7e0.js"><link rel="prefetch" href="/assets/js/570.97ff6423.js"><link rel="prefetch" href="/assets/js/571.de1377cd.js"><link rel="prefetch" href="/assets/js/572.48b8400b.js"><link rel="prefetch" href="/assets/js/573.8251ebaf.js"><link rel="prefetch" href="/assets/js/574.ec3d6c1e.js"><link rel="prefetch" href="/assets/js/575.b0d429a1.js"><link rel="prefetch" href="/assets/js/576.98ce9170.js"><link rel="prefetch" href="/assets/js/577.85fc2017.js"><link rel="prefetch" href="/assets/js/578.1393ac7f.js"><link rel="prefetch" href="/assets/js/579.1340e178.js"><link rel="prefetch" href="/assets/js/58.85ac2740.js"><link rel="prefetch" href="/assets/js/580.a5979445.js"><link rel="prefetch" href="/assets/js/581.effdc269.js"><link rel="prefetch" href="/assets/js/582.13276063.js"><link rel="prefetch" href="/assets/js/583.357a2443.js"><link rel="prefetch" href="/assets/js/584.338ef731.js"><link rel="prefetch" href="/assets/js/585.87932741.js"><link rel="prefetch" href="/assets/js/586.b1d6e000.js"><link rel="prefetch" href="/assets/js/587.ed5b8377.js"><link rel="prefetch" href="/assets/js/588.16d2c418.js"><link rel="prefetch" href="/assets/js/589.c96fd7fa.js"><link rel="prefetch" href="/assets/js/59.0b225757.js"><link rel="prefetch" href="/assets/js/590.1a06b2a0.js"><link rel="prefetch" href="/assets/js/591.0efd886c.js"><link rel="prefetch" href="/assets/js/592.b22d0ef5.js"><link rel="prefetch" href="/assets/js/593.84ed6ebe.js"><link rel="prefetch" href="/assets/js/594.cd721b88.js"><link rel="prefetch" href="/assets/js/595.2400817a.js"><link rel="prefetch" href="/assets/js/596.f2c512d1.js"><link rel="prefetch" href="/assets/js/597.df10ec57.js"><link rel="prefetch" href="/assets/js/598.61ef6a7d.js"><link rel="prefetch" href="/assets/js/599.0bebb562.js"><link rel="prefetch" href="/assets/js/6.29d112b1.js"><link rel="prefetch" href="/assets/js/60.3bbab51d.js"><link rel="prefetch" href="/assets/js/600.1ea7596f.js"><link rel="prefetch" href="/assets/js/601.f2642bb3.js"><link rel="prefetch" href="/assets/js/602.400cc987.js"><link rel="prefetch" href="/assets/js/603.db77d173.js"><link rel="prefetch" href="/assets/js/604.0ffa30ae.js"><link rel="prefetch" href="/assets/js/605.32ca9607.js"><link rel="prefetch" href="/assets/js/606.8c1e5683.js"><link rel="prefetch" href="/assets/js/607.9c9463ef.js"><link rel="prefetch" href="/assets/js/608.d5efa77e.js"><link rel="prefetch" href="/assets/js/609.5bbffb7d.js"><link rel="prefetch" href="/assets/js/61.f7301486.js"><link rel="prefetch" href="/assets/js/610.5b0fc9da.js"><link rel="prefetch" href="/assets/js/611.bb8058c9.js"><link rel="prefetch" href="/assets/js/612.b0788f43.js"><link rel="prefetch" href="/assets/js/613.49511e65.js"><link rel="prefetch" href="/assets/js/614.fae3eacf.js"><link rel="prefetch" href="/assets/js/615.359e5899.js"><link rel="prefetch" href="/assets/js/616.905132f4.js"><link rel="prefetch" href="/assets/js/617.6c8c594e.js"><link rel="prefetch" href="/assets/js/618.6ac4f3ff.js"><link rel="prefetch" href="/assets/js/619.411a0c14.js"><link rel="prefetch" href="/assets/js/62.9aeede84.js"><link rel="prefetch" href="/assets/js/620.56155724.js"><link rel="prefetch" href="/assets/js/621.149d7933.js"><link rel="prefetch" href="/assets/js/622.f0c9bc08.js"><link rel="prefetch" href="/assets/js/623.e0989fbe.js"><link rel="prefetch" href="/assets/js/624.80b1f6a9.js"><link rel="prefetch" href="/assets/js/625.a0a392c4.js"><link rel="prefetch" href="/assets/js/626.56fd4e8a.js"><link rel="prefetch" href="/assets/js/627.264e7313.js"><link rel="prefetch" href="/assets/js/628.7a868b19.js"><link rel="prefetch" href="/assets/js/629.e7e15da1.js"><link rel="prefetch" href="/assets/js/63.4df55c8b.js"><link rel="prefetch" href="/assets/js/630.998c9b61.js"><link rel="prefetch" href="/assets/js/631.69aa049c.js"><link rel="prefetch" href="/assets/js/632.da3882c1.js"><link rel="prefetch" href="/assets/js/633.0d49adf0.js"><link rel="prefetch" href="/assets/js/634.f9984ede.js"><link rel="prefetch" href="/assets/js/635.f0439f65.js"><link rel="prefetch" href="/assets/js/636.54de4194.js"><link rel="prefetch" href="/assets/js/637.55d1c226.js"><link rel="prefetch" href="/assets/js/638.2a9a510f.js"><link rel="prefetch" href="/assets/js/639.8bec360e.js"><link rel="prefetch" href="/assets/js/64.7f3e5e81.js"><link rel="prefetch" href="/assets/js/640.30d7f96e.js"><link rel="prefetch" href="/assets/js/641.68100098.js"><link rel="prefetch" href="/assets/js/642.4b793817.js"><link rel="prefetch" href="/assets/js/643.d86a81ea.js"><link rel="prefetch" href="/assets/js/644.76327ea2.js"><link rel="prefetch" href="/assets/js/645.0ac6a923.js"><link rel="prefetch" href="/assets/js/646.cd92f728.js"><link rel="prefetch" href="/assets/js/647.f2c624e1.js"><link rel="prefetch" href="/assets/js/648.c8f3b955.js"><link rel="prefetch" href="/assets/js/649.6370753b.js"><link rel="prefetch" href="/assets/js/65.2beeae9b.js"><link rel="prefetch" href="/assets/js/650.afe31909.js"><link rel="prefetch" href="/assets/js/651.87971ec0.js"><link rel="prefetch" href="/assets/js/652.0adf10a6.js"><link rel="prefetch" href="/assets/js/653.1f655726.js"><link rel="prefetch" href="/assets/js/654.53e24c7c.js"><link rel="prefetch" href="/assets/js/655.c95a66ea.js"><link rel="prefetch" href="/assets/js/656.38b5a5ea.js"><link rel="prefetch" href="/assets/js/657.3167aa94.js"><link rel="prefetch" href="/assets/js/658.7c40ff62.js"><link rel="prefetch" href="/assets/js/659.5d2b9b54.js"><link rel="prefetch" href="/assets/js/66.44b214db.js"><link rel="prefetch" href="/assets/js/660.96a8da9e.js"><link rel="prefetch" href="/assets/js/661.4de3b6c1.js"><link rel="prefetch" href="/assets/js/662.7d9bf181.js"><link rel="prefetch" href="/assets/js/663.4ccaf40a.js"><link rel="prefetch" href="/assets/js/664.7ab4fa53.js"><link rel="prefetch" href="/assets/js/665.32245d26.js"><link rel="prefetch" href="/assets/js/666.e6617151.js"><link rel="prefetch" href="/assets/js/667.fb3b0547.js"><link rel="prefetch" href="/assets/js/668.3d1b2e36.js"><link rel="prefetch" href="/assets/js/669.b769905f.js"><link rel="prefetch" href="/assets/js/67.c31aaacf.js"><link rel="prefetch" href="/assets/js/670.88ed3af3.js"><link rel="prefetch" href="/assets/js/671.1aff6bfe.js"><link rel="prefetch" href="/assets/js/672.c90888f7.js"><link rel="prefetch" href="/assets/js/673.81241fdc.js"><link rel="prefetch" href="/assets/js/674.838a424d.js"><link rel="prefetch" href="/assets/js/675.603ac896.js"><link rel="prefetch" href="/assets/js/676.ff44b5dc.js"><link rel="prefetch" href="/assets/js/677.41a8087a.js"><link rel="prefetch" href="/assets/js/678.f0eb8d04.js"><link rel="prefetch" href="/assets/js/679.db78199e.js"><link rel="prefetch" href="/assets/js/68.08607c89.js"><link rel="prefetch" href="/assets/js/680.8fded9d4.js"><link rel="prefetch" href="/assets/js/681.0b019dfd.js"><link rel="prefetch" href="/assets/js/682.746e9190.js"><link rel="prefetch" href="/assets/js/683.d5c00845.js"><link rel="prefetch" href="/assets/js/684.b3207cad.js"><link rel="prefetch" href="/assets/js/685.d78f18ba.js"><link rel="prefetch" href="/assets/js/686.5aaecd19.js"><link rel="prefetch" href="/assets/js/687.821a06f4.js"><link rel="prefetch" href="/assets/js/688.8bbc1890.js"><link rel="prefetch" href="/assets/js/689.79b49e8c.js"><link rel="prefetch" href="/assets/js/69.509245d6.js"><link rel="prefetch" href="/assets/js/690.fa62f9dd.js"><link rel="prefetch" href="/assets/js/691.d3eb1a60.js"><link rel="prefetch" href="/assets/js/692.738f14bb.js"><link rel="prefetch" href="/assets/js/693.e6a497a2.js"><link rel="prefetch" href="/assets/js/694.6eea1c54.js"><link rel="prefetch" href="/assets/js/695.88e6acee.js"><link rel="prefetch" href="/assets/js/696.de10297f.js"><link rel="prefetch" href="/assets/js/697.96d04062.js"><link rel="prefetch" href="/assets/js/698.200cc84f.js"><link rel="prefetch" href="/assets/js/699.f11fc627.js"><link rel="prefetch" href="/assets/js/7.c16198b8.js"><link rel="prefetch" href="/assets/js/70.5f6285b1.js"><link rel="prefetch" href="/assets/js/700.93d48f59.js"><link rel="prefetch" href="/assets/js/701.b861f29a.js"><link rel="prefetch" href="/assets/js/702.dc82e05c.js"><link rel="prefetch" href="/assets/js/703.625ce87c.js"><link rel="prefetch" href="/assets/js/704.005a0a7c.js"><link rel="prefetch" href="/assets/js/705.16ad9230.js"><link rel="prefetch" href="/assets/js/706.4eead30a.js"><link rel="prefetch" href="/assets/js/707.730306ce.js"><link rel="prefetch" href="/assets/js/708.e37e743a.js"><link rel="prefetch" href="/assets/js/709.f2144f52.js"><link rel="prefetch" href="/assets/js/71.d5d0dae5.js"><link rel="prefetch" href="/assets/js/710.ee9d9b17.js"><link rel="prefetch" href="/assets/js/711.2813c338.js"><link rel="prefetch" href="/assets/js/712.e801bce4.js"><link rel="prefetch" href="/assets/js/713.21fc267c.js"><link rel="prefetch" href="/assets/js/714.4ec35525.js"><link rel="prefetch" href="/assets/js/715.fdc3cc84.js"><link rel="prefetch" href="/assets/js/716.324932ad.js"><link rel="prefetch" href="/assets/js/717.a072a66f.js"><link rel="prefetch" href="/assets/js/718.575e0305.js"><link rel="prefetch" href="/assets/js/719.5fdd55de.js"><link rel="prefetch" href="/assets/js/72.25648231.js"><link rel="prefetch" href="/assets/js/720.61477f23.js"><link rel="prefetch" href="/assets/js/721.c8bdab00.js"><link rel="prefetch" href="/assets/js/722.9255f385.js"><link rel="prefetch" href="/assets/js/723.f27792a4.js"><link rel="prefetch" href="/assets/js/724.cd504f88.js"><link rel="prefetch" href="/assets/js/725.55c845fa.js"><link rel="prefetch" href="/assets/js/726.bb49a9db.js"><link rel="prefetch" href="/assets/js/727.53d587fe.js"><link rel="prefetch" href="/assets/js/728.86972387.js"><link rel="prefetch" href="/assets/js/729.02fa0263.js"><link rel="prefetch" href="/assets/js/73.29902c1f.js"><link rel="prefetch" href="/assets/js/730.817156d1.js"><link rel="prefetch" href="/assets/js/731.6f5d6735.js"><link rel="prefetch" href="/assets/js/732.9a5ff781.js"><link rel="prefetch" href="/assets/js/733.bcb13916.js"><link rel="prefetch" href="/assets/js/734.11482b19.js"><link rel="prefetch" href="/assets/js/735.8c0d62f3.js"><link rel="prefetch" href="/assets/js/736.06465705.js"><link rel="prefetch" href="/assets/js/737.79741d87.js"><link rel="prefetch" href="/assets/js/738.cad12ff9.js"><link rel="prefetch" href="/assets/js/739.cb09e552.js"><link rel="prefetch" href="/assets/js/74.bcf666a0.js"><link rel="prefetch" href="/assets/js/740.029b1950.js"><link rel="prefetch" href="/assets/js/741.b937216a.js"><link rel="prefetch" href="/assets/js/742.ec02c706.js"><link rel="prefetch" href="/assets/js/743.70a96283.js"><link rel="prefetch" href="/assets/js/744.909b870c.js"><link rel="prefetch" href="/assets/js/745.80defb2d.js"><link rel="prefetch" href="/assets/js/746.fb972248.js"><link rel="prefetch" href="/assets/js/747.1a7b52fd.js"><link rel="prefetch" href="/assets/js/748.036f5d16.js"><link rel="prefetch" href="/assets/js/749.bd9a413d.js"><link rel="prefetch" href="/assets/js/75.23c5a54d.js"><link rel="prefetch" href="/assets/js/750.3f1ae8f5.js"><link rel="prefetch" href="/assets/js/751.8897bc9f.js"><link rel="prefetch" href="/assets/js/752.9b387659.js"><link rel="prefetch" href="/assets/js/753.f411ce21.js"><link rel="prefetch" href="/assets/js/754.26def684.js"><link rel="prefetch" href="/assets/js/755.923aff62.js"><link rel="prefetch" href="/assets/js/756.9965743a.js"><link rel="prefetch" href="/assets/js/757.0c6bbbfd.js"><link rel="prefetch" href="/assets/js/758.a830f8b1.js"><link rel="prefetch" href="/assets/js/759.987cad77.js"><link rel="prefetch" href="/assets/js/76.f54f3d4f.js"><link rel="prefetch" href="/assets/js/760.9f2652a0.js"><link rel="prefetch" href="/assets/js/761.df01a0ee.js"><link rel="prefetch" href="/assets/js/762.e0c05a1a.js"><link rel="prefetch" href="/assets/js/763.da8a60bd.js"><link rel="prefetch" href="/assets/js/764.9f2a2830.js"><link rel="prefetch" href="/assets/js/765.44e61161.js"><link rel="prefetch" href="/assets/js/766.cd7da8c1.js"><link rel="prefetch" href="/assets/js/767.6ea1fea2.js"><link rel="prefetch" href="/assets/js/768.91529b8f.js"><link rel="prefetch" href="/assets/js/769.194d7a3e.js"><link rel="prefetch" href="/assets/js/77.3d43a163.js"><link rel="prefetch" href="/assets/js/770.227fd5b9.js"><link rel="prefetch" href="/assets/js/771.44d5e37e.js"><link rel="prefetch" href="/assets/js/772.234d9bf6.js"><link rel="prefetch" href="/assets/js/773.ff1dfb6a.js"><link rel="prefetch" href="/assets/js/774.d401364f.js"><link rel="prefetch" href="/assets/js/775.37a7cf41.js"><link rel="prefetch" href="/assets/js/776.0cd10853.js"><link rel="prefetch" href="/assets/js/777.599a3a48.js"><link rel="prefetch" href="/assets/js/778.eef27a95.js"><link rel="prefetch" href="/assets/js/779.29351199.js"><link rel="prefetch" href="/assets/js/78.fd0780ac.js"><link rel="prefetch" href="/assets/js/780.74caed94.js"><link rel="prefetch" href="/assets/js/781.ee0fa9b5.js"><link rel="prefetch" href="/assets/js/782.7cffed09.js"><link rel="prefetch" href="/assets/js/783.7f01f518.js"><link rel="prefetch" href="/assets/js/784.5f65e3d7.js"><link rel="prefetch" href="/assets/js/785.d7e13880.js"><link rel="prefetch" href="/assets/js/786.6110d12f.js"><link rel="prefetch" href="/assets/js/787.334a5cdd.js"><link rel="prefetch" href="/assets/js/788.f261bc71.js"><link rel="prefetch" href="/assets/js/789.b6d74f7d.js"><link rel="prefetch" href="/assets/js/79.7b5d6224.js"><link rel="prefetch" href="/assets/js/790.fa948ec4.js"><link rel="prefetch" href="/assets/js/791.22080013.js"><link rel="prefetch" href="/assets/js/792.31ce806c.js"><link rel="prefetch" href="/assets/js/793.9fd0c56f.js"><link rel="prefetch" href="/assets/js/794.44a8cd9c.js"><link rel="prefetch" href="/assets/js/795.9f8346e5.js"><link rel="prefetch" href="/assets/js/796.0de9c7a1.js"><link rel="prefetch" href="/assets/js/797.83ac32a6.js"><link rel="prefetch" href="/assets/js/798.393fc81d.js"><link rel="prefetch" href="/assets/js/799.c1fb3981.js"><link rel="prefetch" href="/assets/js/8.3275ed06.js"><link rel="prefetch" href="/assets/js/80.df3f0a1f.js"><link rel="prefetch" href="/assets/js/800.2832adeb.js"><link rel="prefetch" href="/assets/js/801.04c58fc4.js"><link rel="prefetch" href="/assets/js/81.3d90ef6b.js"><link rel="prefetch" href="/assets/js/82.2d42448d.js"><link rel="prefetch" href="/assets/js/83.900b4de2.js"><link rel="prefetch" href="/assets/js/84.ff570f67.js"><link rel="prefetch" href="/assets/js/85.e3b3af39.js"><link rel="prefetch" href="/assets/js/86.17b5aedc.js"><link rel="prefetch" href="/assets/js/87.f931d1d6.js"><link rel="prefetch" href="/assets/js/88.d55863cd.js"><link rel="prefetch" href="/assets/js/89.15a9a6d7.js"><link rel="prefetch" href="/assets/js/9.04948a9d.js"><link rel="prefetch" href="/assets/js/90.22696aa9.js"><link rel="prefetch" href="/assets/js/91.f1bd8a2e.js"><link rel="prefetch" href="/assets/js/92.85733094.js"><link rel="prefetch" href="/assets/js/93.59bacfd7.js"><link rel="prefetch" href="/assets/js/94.a5f9b7a0.js"><link rel="prefetch" href="/assets/js/95.be52d65a.js"><link rel="prefetch" href="/assets/js/96.0b76ba8e.js"><link rel="prefetch" href="/assets/js/97.28183cd4.js"><link rel="prefetch" href="/assets/js/98.2d22829c.js"><link rel="prefetch" href="/assets/js/99.ed602a20.js">
    <link rel="stylesheet" href="/assets/css/0.styles.a3df589e.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">AJ</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">主页</a></div><div class="nav-item"><a href="/op/" class="nav-link">Devops</a></div><div class="nav-item"><a href="/golang/" class="nav-link">Go</a></div><div class="nav-item"><a href="/go-block/" class="nav-link router-link-active">区块链</a></div><div class="nav-item"><a href="/k8s/" class="nav-link">k8s</a></div><div class="nav-item"><a href="/about.html" class="nav-link">关于我</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="更多" class="dropdown-title"><span class="title">更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/go-learning/" class="nav-link">go-learning</a></li><li class="dropdown-item"><!----> <a href="/post/flutter-guide/" class="nav-link">flutter</a></li><li class="dropdown-item"><!----> <a href="/mysql/" class="nav-link">mysql</a></li><li class="dropdown-item"><!----> <a href="/python/" class="nav-link">python</a></li></ul></div></div> <a href="https://github.com/ChinaArJun" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">主页</a></div><div class="nav-item"><a href="/op/" class="nav-link">Devops</a></div><div class="nav-item"><a href="/golang/" class="nav-link">Go</a></div><div class="nav-item"><a href="/go-block/" class="nav-link router-link-active">区块链</a></div><div class="nav-item"><a href="/k8s/" class="nav-link">k8s</a></div><div class="nav-item"><a href="/about.html" class="nav-link">关于我</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="更多" class="dropdown-title"><span class="title">更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/go-learning/" class="nav-link">go-learning</a></li><li class="dropdown-item"><!----> <a href="/post/flutter-guide/" class="nav-link">flutter</a></li><li class="dropdown-item"><!----> <a href="/mysql/" class="nav-link">mysql</a></li><li class="dropdown-item"><!----> <a href="/python/" class="nav-link">python</a></li></ul></div></div> <a href="https://github.com/ChinaArJun" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav> <div></div> <div style="padding-left:1.5rem;"><div></div></div> <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>区块链基础</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/go-block/blockchain/encryption-expand.html" aria-current="page" class="active sidebar-link">常见的密码技术</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go-block/blockchain/encryption-expand.html#_2-对称加密" class="sidebar-link">2.  对称加密</a></li><li class="sidebar-sub-header"><a href="/go-block/blockchain/encryption-expand.html#_3-分组密码的模式" class="sidebar-link">3. 分组密码的模式</a></li><li class="sidebar-sub-header"><a href="/go-block/blockchain/encryption-expand.html#_4-非对称加密" class="sidebar-link">4. 非对称加密</a></li><li class="sidebar-sub-header"><a href="/go-block/blockchain/encryption-expand.html#_5-单向散列函数" class="sidebar-link">5. 单向散列函数</a></li><li class="sidebar-sub-header"><a href="/go-block/blockchain/encryption-expand.html#_6-消息认证码" class="sidebar-link">6. 消息认证码</a></li><li class="sidebar-sub-header"><a href="/go-block/blockchain/encryption-expand.html#_7-数字签名" class="sidebar-link">7. 数字签名</a></li><li class="sidebar-sub-header"><a href="/go-block/blockchain/encryption-expand.html#_8-证书" class="sidebar-link">8. 证书</a></li><li class="sidebar-sub-header"><a href="/go-block/blockchain/encryption-expand.html#_9-ssl-tls" class="sidebar-link">9. SSL/TLS</a></li><li class="sidebar-sub-header"><a href="/go-block/blockchain/encryption-expand.html#参考资料" class="sidebar-link">参考资料</a></li></ul></li><li><a href="/go-block/blockchain/encryption.html" class="sidebar-link">密码学安全</a></li><li><a href="/go-block/blockchain/ethereum.html" class="sidebar-link">以太坊智能合约</a></li><li><a href="/go-block/blockchain/hyperledgerfabric.html" class="sidebar-link">hyperledger fabric</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>Hyperledger Fabric 1.2</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/go-block/fabric/1搭建网络-纯手动.html" class="sidebar-link">1搭建网络-纯手动</a></li><li><a href="/go-block/fabric/1环境搭建.html" class="sidebar-link">1环境搭建</a></li><li><a href="/go-block/fabric/2Fabric介绍.html" class="sidebar-link">2Fabric介绍</a></li><li><a href="/go-block/fabric/3Fabric核心模块.html" class="sidebar-link">3Fabric核心模块</a></li><li><a href="/go-block/fabric/4网络搭建-脚本.html" class="sidebar-link">4网络搭建-脚本</a></li><li><a href="/go-block/fabric/5智能合约.html" class="sidebar-link">5智能合约</a></li><li><a href="/go-block/fabric/6Fabric账号.html" class="sidebar-link">6Fabric账号</a></li><li><a href="/go-block/fabric/7Solo多机多节点部署.html" class="sidebar-link">7Solo多机多节点部署</a></li><li><a href="/go-block/fabric/8Kafka多机多节点部署.html" class="sidebar-link">8Kafka多机多节点部署</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>Hyperledger Fabric 2.X</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/go-block/fabric2.x/" class="sidebar-link">2.X目录</a></li></ul></section></li></ul> </aside> <!----> <!----> <main class="page"><div class="theme-default-content" style="margin-bottom:-5rem;"><div class="bar"><div class="bar-intro"><div class="text">
      流逝的是岁月，不变的是情怀.
        </div> <div class="text">
      坚持学习，是为了成就更好的自己. <br></div> <div>公众号[中关村程序员]</div></div></div> <!----></div> <div class="theme-default-content content__default"><p>[TOC]</p> <p>#常用的密码技术</p> <p>##1 密码</p> <h3 id="_1-1-发送者、接收者和窃听者"><a href="#_1-1-发送者、接收者和窃听者" class="header-anchor">#</a> 1.1 发送者、接收者和窃听者</h3> <blockquote><p>请想象一个Alice向Bob发送电子邮件的场景。在这个场景中，发出邮件的Alice称为 <strong>发送者（sender）</strong>，而收到邮件的Bob则称为 <strong>接收者（receiver）</strong>。</p> <p>在讲解发送者、接收者的概念时，用邮件这个例子会比较便于理解，但实际上发送者和接收者这两个术语的使用范围并不仅仅局限于邮件。当某个人向另一个人发送信息时，发出信息的人称为发送者，而收到信息的人称为接收者。另外，被发送的信息有时也统称为 <strong>消息（message）</strong>。</p></blockquote> <ul><li><p><strong>Alice向Bob发送邮件</strong></p> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538728596743.png" alt="1538728596743"></p></li></ul> <blockquote><p>邮件是通过互联网从Alice的计算机发送到Bob的计算机的。在发送邮件时，邮件会经过许多台计算机和通信设备进行中转，在这个过程中，就存在被恶意窃听者（eavesdropper）偷看到的可能性。</p></blockquote> <ul><li><strong>Eve（窃听者）看到邮件的内容</strong></li></ul> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538728627477.png" alt="1538728627477"></p> <blockquote><p>窃听者Eve并不一定是人类，有可能是安装在通信设备上的某种窃听器，也可能是安装在邮件软件和邮件服务器上的某些程序。</p> <p>尽管邮件内容原本应该只有发送者和接收者两个人知道，但如果不采取相应的对策，就存在被第三方知道的风险。</p></blockquote> <h3 id="_1-2-加密和解密"><a href="#_1-2-加密和解密" class="header-anchor">#</a> 1.2 加密和解密</h3> <blockquote><p>Alice不想让别人看到邮件的内容，于是她决定将邮件进行**加密（encrypt）**后再发送出去。</p> <p>加密之前的消息称为<strong>明文（plaintext）</strong>，加密之后的消息称为<strong>密文（cipher-text）</strong>。</p> <p>我们看到明文可以理解其中的含义，而看到密文则无法理解其中的含义。</p></blockquote> <ul><li><strong>明文加密之后就会变成看不懂的密文</strong></li></ul> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538728678781.png" alt="1538728678781"></p> <blockquote><p>Bob收到了来自Alice的加密邮件，但作为接收者的Bob也是无法直接阅读密文的，于是
Bob需要对密文进行解密（decrypt）之后再阅读。解密就是将密文恢复成明文的过程。</p></blockquote> <ul><li><p><strong>密文解密之后就变成了原来的明文</strong></p> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538728746711.png" alt="1538728746711"></p></li></ul> <blockquote><p>将消息加密后发送的话，即使消息被窃听，窃听者得到的也只是密文，而无法得知加密前的明文内容</p></blockquote> <ul><li><p><strong>将消息加密后发送, 窃听者只能得到密文</strong></p> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538728869719.png" alt="1538728869719"></p> <blockquote><p>在上述场景中，Alice将邮件进行加密，而Bob则进行解密，这样做的目的，是为了不让窃听者Eve读取邮件的内容Alice和Bob通过运用<strong>密码(cryptography)技术</strong>，保证了邮件的<strong>机密性（confidentiality）</strong>。</p></blockquote></li></ul> <h3 id="_1-3-秘钥"><a href="#_1-3-秘钥" class="header-anchor">#</a> 1.3 秘钥</h3> <h4 id="_1-3-1-密码算法"><a href="#_1-3-1-密码算法" class="header-anchor">#</a> 1.3.1 密码算法</h4> <blockquote><p>用于解决复杂问题的步骤，通常称为<strong>算法（algorithm）</strong>。从明文生成密文的步骤，也就是加密的步骤，称为“加密算法&quot;，而解密的步骤则称为“解密算法&quot;。加密、解密的算法合在一起统称为<strong>密码算法</strong>。</p></blockquote> <h4 id="_1-3-2-秘钥"><a href="#_1-3-2-秘钥" class="header-anchor">#</a> 1.3.2 秘钥</h4> <blockquote><p>密码算法中需要<strong>密钥（key）</strong>。现实世界中的“钥''，是像 🔑 这样的形状微妙而复杂的小金属片。然而，密码算法中的密钥，则是像203554728568477650354673080689430768这样的一串非常大的数字。</p></blockquote> <ul><li><p><strong>加密、解密与秘钥</strong></p> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538728964328.png" alt="1538728964328"></p></li></ul> <blockquote><p>无论是在加密时还是在解密时，都需要知道密钥。</p> <p>正如保险柜的钥匙可以保护保险柜中存放的贵重物品一样，密码中的密钥可以保护你的重要数据。即使保险箱再坚固，如果钥匙被盗, 里面的贵重物品也会被盗。同样地我们也必须注意不要让密码的密钥被他人窃取。</p></blockquote> <h3 id="_1-4-凯撒密码"><a href="#_1-4-凯撒密码" class="header-anchor">#</a> 1.4 凯撒密码</h3> <blockquote><p>恺撒密码（Caesar cipher）是一种相传尤利乌斯·恺撒曾使用过的密码。恺撒于公元前100年左右诞生于古罗马，是一位著名的军事统帅。</p> <p><font color="red">恺撤密码是通过将明文中所使用的字母表按照一定的字数“平移”来进行加密的</font>。比如在日语（例如平假名）或者汉语（例如汉语拼音）或者英文字母表中都可以用同样的思路来实现恺撒密码。</p> <p>为了讲解方便，我们用小写字母（a，b，c，…）来表小明文，用大写字母（A，B，C，...）来表示密文。</p> <p>现在我们将字母表平移3个字母，于是，明文中的a在加密后就变成了与其相隔3个字母的D，以此类推。b变成E，c变成F，d变成G......v变成Y，w变成Z，而x则会回到字母表的开头而变成A，相应地，y变成B，z变成C。通过下图我们可以很容易地理解“平移&quot;的具体工作方式。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538792314838.png" alt="1538792314838"></p> <h4 id="_1-4-1-凯撒密码的加密"><a href="#_1-4-1-凯撒密码的加密" class="header-anchor">#</a> 1.4.1 凯撒密码的加密</h4> <blockquote><p>这里，我们假设要保密的信息为<strong>monkey d luffy</strong>这个男孩的名字。我们暂且不管这个名字到底代表一位真实的男性，还是只是一种暗号，只考虑将它在保密的状态下发送给接收者。</p> <p>此时，明文包含下列12个字母：<strong>monkey d luffy</strong>, 接下来我们对明文中的字母逐一加密:</p></blockquote> <div class="language-c extra-class"><pre class="language-c"><code>                                m	<span class="token operator">--</span><span class="token operator">-&gt;</span>	P				
                                o	<span class="token operator">--</span><span class="token operator">-&gt;</span>	R
                                n	<span class="token operator">--</span><span class="token operator">-&gt;</span>	Q
                                k	<span class="token operator">--</span><span class="token operator">-&gt;</span>	N
                                e	<span class="token operator">--</span><span class="token operator">-&gt;</span>	H
                                y	<span class="token operator">--</span><span class="token operator">-&gt;</span>	B
                                d	<span class="token operator">--</span><span class="token operator">-&gt;</span>	G
                                l	<span class="token operator">--</span><span class="token operator">-&gt;</span>	O
                                u	<span class="token operator">--</span><span class="token operator">-&gt;</span>	X
                                f	<span class="token operator">--</span><span class="token operator">-&gt;</span>	I
                                f	<span class="token operator">--</span><span class="token operator">-&gt;</span>	I
                                y	<span class="token operator">--</span><span class="token operator">-&gt;</span>	B
</code></pre></div><blockquote><p>这样，明文 monkey d luffy 就被转换成了密文PRQNHB G OXIIB，monkey d luffy这个词我们能够看懂，但</p> <p>PRQNHB G OXIIB就看不懂了。</p> <p>恺撒密码中，将字母表中的字母平移这个操作就是密码的算法，而平移的字母数量则相当于密钥。在上面的例子中，密钥为3（如下图）。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538795708168.png" alt="1538795708168"></p> <h4 id="_1-4-2-凯撒密码的解密"><a href="#_1-4-2-凯撒密码的解密" class="header-anchor">#</a> 1.4.2 凯撒密码的解密</h4> <blockquote><p>现在，假设接收者已经收到了密文PRQNHB G OXIIB，由于密文本身是看不懂的，因此必须将它解密成明文。</p> <p>恺撒密码的解密过程是使用与加密时相同的密钥进行反向的平移操作。用刚才的例子来说，只要反向平移3个字母就可以解密了。</p></blockquote> <div class="language-c extra-class"><pre class="language-c"><code>                                P	<span class="token operator">--</span><span class="token operator">-&gt;</span>	m				
                                R	<span class="token operator">--</span><span class="token operator">-&gt;</span>	o
                                Q	<span class="token operator">--</span><span class="token operator">-&gt;</span>	n
                                N	<span class="token operator">--</span><span class="token operator">-&gt;</span>	k
                                H	<span class="token operator">--</span><span class="token operator">-&gt;</span>	e
                                B	<span class="token operator">--</span><span class="token operator">-&gt;</span>	y
                                G	<span class="token operator">--</span><span class="token operator">-&gt;</span>	d
                                O	<span class="token operator">--</span><span class="token operator">-&gt;</span>	l
                                X	<span class="token operator">--</span><span class="token operator">-&gt;</span>	u
                                I	<span class="token operator">--</span><span class="token operator">-&gt;</span>	f
                                I	<span class="token operator">--</span><span class="token operator">-&gt;</span>	f
                                B	<span class="token operator">--</span><span class="token operator">-&gt;</span>	y
</code></pre></div><blockquote><p>这样我们就得到了明文monkey d luffy。</p> <p>在这个场景中， 秘钥3必须由发送者和接收者事先约定好。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538796488394.png" alt="1538796488394"></p> <h3 id="_1-5-密码信息安全常识与威胁"><a href="#_1-5-密码信息安全常识与威胁" class="header-anchor">#</a> 1.5 密码信息安全常识与威胁</h3> <h4 id="_1-5-1-密码信息安全常识"><a href="#_1-5-1-密码信息安全常识" class="header-anchor">#</a> 1.5.1 密码信息安全常识</h4> <blockquote><p>在继续下面的内容之前，我们先来介绍一些关于密码的常识。刚刚开始学习密码的人常常会对以下这几条感到不可思议，因为它们有悖于我们的一般性常识。</p></blockquote> <ul><li>不要使用保密的密码算法</li> <li>使用低强度的密码比不进行任何加密更危险</li> <li>任何密码总有一天都会被破解</li> <li>密码只是信息安全的一部分</li></ul> <h5 id="不要使用保密的密码算法"><a href="#不要使用保密的密码算法" class="header-anchor">#</a> 不要使用保密的密码算法</h5> <blockquote><p>很多企业都有下面这样的想法：</p> <p>“由公司自己开发一种密码算法，并将这种算法保密，这样就能保证安全。然而，这样的想法却是大错特错，使用保密的密码算法是无法获得高安全性的。我们不应该制作或使用任何保密的密码算法，而是应该使用那些已经公开的、被公认为强度较高的密码算法。</p> <p>这样做的原因主要有以下两点：</p></blockquote> <ul><li><p><strong>密码算法的秘密早晚会公诸于世</strong></p> <blockquote><p>从历史上看，密码算法的秘密最终无一例外地都会被暴露出来。例如: RSA公司开发的RC4密码算法曾经也是保密的，但最终还是有一位匿名人士开发并公开了与其等效的程序。</p> <p>一旦密码算法的详细信息被暴露，依靠对密码算法本身进行保密来确保机密性的密码系统也就土崩瓦解了。反之，那些公开的算法从一开始就没有设想过要保密，因此算法的暴露丝毫不会削弱它们的强度。</p></blockquote></li> <li><p><strong>开发高强度的密码算法是非常困难的</strong></p> <blockquote><ul><li>要比较密码算法的强弱是极其困难的，因为密码算法的强度并不像数学那样可以进行严密的证明。密码算法的强度只能通过事实来证明，如果专业密码破译者经过数年的尝试仍然没有破解某个密码算法，则说明这种算法的强度较高。</li> <li>稍微聪明一点的程序员很容易就能够编写出“自己的密码系统&quot;。这样的密码在外行看来貌似牢不可破，但在专业密码破译者的眼里，要破解这样的密码几乎是手到擒来。</li> <li>现在世界上公开的被认为强度较高的密码算法，几乎都是经过密码破译者长期尝试破解未果而存活下来的。因此，如果认为“公司自己开发的密码系统比那些公开的密码系统更强”，那只能说是过于高估自己公司的能力了。</li> <li>试图通过对密码算法本身进行保密来确保安全性的行为，一般称为隐蔽式安全性（securitybyobscurity），这种行为是危险且愚蠢的。</li> <li>反过来说，将密码算法的详细信息以及程序源代码全部交给专业密码破译者，并且为其提供大量的明文和密文样本，如果在这样的情况下破译一段新的密文依然需要花上相当长的时间，就说明这是高强度的密码。</li></ul></blockquote></li></ul> <p>#####使用低强度的密码比不进行任何加密更危险</p> <blockquote><p>一般人们会认为．就算密码的强度再低，也比完全不加密要强吧？其实这样的想法是非常危险的。</p> <p>**正确的想法应该是：**与其使用低强度的密码，还不如从一开始就不使用任何密码这主要是由于用户容易通过“密码”这个词获得一种“错误的安全感”。对于用户来说，安全感与密码的强度无关，而只是由“信息已经被加密了”这一事实产生的，而这通常会导致用户在处理一些机密信息的时候麻痹大意。</p></blockquote> <p>#####任何密码总有一天会被破译</p> <blockquote><p>如果某种密码产品宣称“本产品使用了绝对不会被破解的密码算法”，那么你就要对这个产品的安全性打个问号了，这是因为绝对不会被破解的密码是不存在的。</p> <p>无论使用任何密码算法所生成的密文，只要将所有可能的密钥全部尝试一遍，就总有一天可以破译出来。因此，破译密文所需要花费的时间，与要保密的明文的价值之间的权衡就显得非常重要。</p></blockquote> <p>#####密码只是信息安全的一部分</p> <blockquote><p>我们还是回到Alice给Bob发送加密邮件的例子。即便不去破解密码算法，也依然有很多方法能够知道Alice所发送的邮件内容, 例如:</p> <p><strong>攻击者可以不去试图破译经过加密的邮件，而是转而攻击Alice的电脑以获取加密之前的邮件明文。</strong></p> <p>上面提到的攻击手段，都与密码的强度毫无关系。要保证良好的安全性，就需要理解“系统”这一概念本身的性质复杂的系统就像一根由无数个环节相连组成的链条，如果用力拉，链条就会从其中最脆弱的环节处断开。因此，系统的强度取决于其中最脆弱的环节的强度。</p> <p>最脆弱的环节并不是密码，而是人类自己。</p></blockquote> <h4 id="_1-5-2-密码信息威胁"><a href="#_1-5-2-密码信息威胁" class="header-anchor">#</a> 1.5.2 密码信息威胁</h4> <blockquote><p>我们将信息安全所面临的威胁与用来用对这些威胁的密码技术直接的关系用一张图标来表示出来。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538814978185.png" alt="1538814978185"></p> <h2 id="_2-对称加密"><a href="#_2-对称加密" class="header-anchor">#</a> 2.  对称加密</h2> <div class="language-go extra-class"><pre class="language-go"><code><span class="token string">&quot;对称加密: 也称为对称密码, 是指在加密和解码时使用同一秘钥的加密方式&quot;</span>
</code></pre></div><p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538729092431.png" alt="1538729092431"></p> <h3 id="_2-1-编码"><a href="#_2-1-编码" class="header-anchor">#</a> 2.1 编码</h3> <blockquote><p>现代的密码都是建立在计算机的基础之上的，这是因为现代的密码所处理的数据量非常大，而且密码算法也非常复杂，不借助计算机的力量就无法完成加密和解密的操作。</p> <p>计算机的操作对象并不是文字，而是由0和1排列而成的<strong>比特序列</strong>。无论是文字、图像、声音、视频还是程序，在计算机中都是用比特序列来表示的。执行加密操作的程序，就是将表示明文的比特序列转换为表示密文的比特序列。</p> <p>将现实世界中的东西映射为比特序列的操作称为<strong>编码（encoding）</strong>。例如midnight（深夜）这个词，我们可以对其中的每个字母逐一进行编码，这种编码规则叫作<strong>ASCII</strong>。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538729172517.png" alt="1538729172517"></p> <blockquote><p>注意这里的m --&gt; 01101101这一转换并不是加密而是编码。尽管在人类看来0和1的序列跟密码没什么两样，但计算机却可以“看懂&quot;这些比特序列，并很快地反应出其所对应的字符 <strong>midnight</strong></p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538729355964.png" alt="1538729355964"></p> <h3 id="_2-2-des"><a href="#_2-2-des" class="header-anchor">#</a> 2.2 DES</h3> <h4 id="_2-2-1-什么是des"><a href="#_2-2-1-什么是des" class="header-anchor">#</a> 2.2.1 什么是DES</h4> <blockquote><p>**DES（Data Encryption Standard）**是1977年美国联邦信息处理标准（FIPS）中所采用的一种对称密码（FIPS46.3）。DES一直以来被美国以及其他国家的政府和银行等广泛使用。然而，随着计算机的进步，现在DES已经能够被暴力破解，强度大不如前了。</p> <p>RSA公司举办过破泽DES密钥的比赛（DESChallenge），我们可以看一看RSA公司官方公布的比赛结果：</p> <ul><li>1997年的DES Challenge1中用了96天破译密钥</li> <li>1998年的DES ChallengeIl-I中用了41天破译密钥</li> <li>1998年的DES ChallengeII-2中用了56小时破译密钥</li> <li>1999年的DES ChallengeIll中只用了22小时15分钟破译密钥</li></ul> <p>由于DES的密文可以在短时间内被破译，因此除了用它来解密以前的密文以外，现在我们不应该再使用DES了。</p></blockquote> <h4 id="_2-2-2-加密和解密"><a href="#_2-2-2-加密和解密" class="header-anchor">#</a> 2.2.2 加密和解密</h4> <blockquote><p>DES是一种将64比特的明文加密成64比特的密文的对称密码算法，==它的密钥长度是56比特==。尽管<font color="red">从规格上来说，DES的密钥长度是64比特，但由于每隔7比特会设置一个用于错误检查的比特，因此实质上其密钥长度是56比特</font>。</p> <p><font color="red">DES是以64比特的明文（比特序列）为一个单位来进行加密的</font>，<strong>这个64比特的单位称为分组</strong>。一般来说，以分组为单位进行处理的密码算法称为<strong>分组密码（blockcipher）</strong>，DES就是分组密码的一种。</p> <p>DES每次只能加密64比特的数据，如果要加密的明文比较长，就需要对DES加密进行迭代（反复），而迭代的具体方式就称为模式（mode）。</p> <p>大B -&gt; bit</p> <p>小b -&gt; byte</p> <p>秘钥长度(56bit + 8bit)/8 = 8byte   12345678</p></blockquote> <ul><li><p><strong>DES的加密与解密 - 图例</strong></p> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538729419926.png" alt="1538729419926"></p></li></ul> <h4 id="_2-2-3-go中对des的操作"><a href="#_2-2-3-go中对des的操作" class="header-anchor">#</a> 2.2.3 Go中对DES的操作</h4> <h5 id="加解密实现思路"><a href="#加解密实现思路" class="header-anchor">#</a> 加解密实现思路</h5> <ul><li><p><strong>加密 - CBC分组模式</strong></p> <blockquote><ol><li>创建并返回一个使用DES算法的cipher.Block接口
<ul><li><strong>秘钥长度为64bit, 即 64/8 = 8字节(byte)</strong></li></ul></li> <li>对最后一个明文分组进行数据填充
<ul><li>DES是以64比特的明文（比特序列）为一个单位来进行加密的</li> <li>最后一组不够64bit, 则需要进行数据填充( <strong>参考第三章</strong>)</li></ul></li> <li>创建一个密码分组为链接模式的, 底层使用DES加密的BlockMode接口</li> <li>加密连续的数据块</li></ol></blockquote></li> <li><p><strong>解密</strong></p> <blockquote><ol><li>创建并返回一个使用DES算法的cipher.Block接口</li> <li>创建一个密码分组为链接模式的, 底层使用DES解密的BlockMode接口</li> <li>数据块解密</li> <li>去掉最后一组的填充数据</li></ol></blockquote></li></ul> <h5 id="加解密的代码实现"><a href="#加解密的代码实现" class="header-anchor">#</a> 加解密的代码实现</h5> <p>在Go中使用DES需要导入的包:</p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;crypto/des&quot;</span>
	<span class="token string">&quot;crypto/cipher&quot;</span>
	<span class="token string">&quot;fmt&quot;</span>
	<span class="token string">&quot;bytes&quot;</span>
<span class="token punctuation">)</span>
</code></pre></div><p>DES加密代码:</p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token comment">// src -&gt; 要加密的明文</span>
<span class="token comment">// key -&gt; 秘钥, 大小为: 8byte</span>
<span class="token keyword">func</span> <span class="token function">DesEncrypt_CBC</span><span class="token punctuation">(</span>src<span class="token punctuation">,</span> key <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">{</span>
	<span class="token comment">// 1. 创建并返回一个使用DES算法的cipher.Block接口</span>
	block<span class="token punctuation">,</span> err <span class="token operator">:=</span> des<span class="token punctuation">.</span><span class="token function">NewCipher</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span>
	<span class="token comment">// 2. 判断是否创建成功</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span><span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// 3. 对最后一个明文分组进行数据填充</span>
	src <span class="token operator">=</span> <span class="token function">PKCS5Padding</span><span class="token punctuation">(</span>src<span class="token punctuation">,</span> block<span class="token punctuation">.</span><span class="token function">BlockSize</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
	<span class="token comment">// 4. 创建一个密码分组为链接模式的, 底层使用DES加密的BlockMode接口</span>
	<span class="token comment">//    参数iv的长度, 必须等于b的块尺寸</span>
	tmp <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token function">byte</span><span class="token punctuation">(</span><span class="token string">&quot;helloAAA&quot;</span><span class="token punctuation">)</span>
	blackMode <span class="token operator">:=</span> cipher<span class="token punctuation">.</span><span class="token function">NewCBCEncrypter</span><span class="token punctuation">(</span>block<span class="token punctuation">,</span> tmp<span class="token punctuation">)</span>
	<span class="token comment">// 5. 加密连续的数据块</span>
	dst <span class="token operator">:=</span> <span class="token function">make</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">,</span> <span class="token function">len</span><span class="token punctuation">(</span>src<span class="token punctuation">)</span><span class="token punctuation">)</span>
	blackMode<span class="token punctuation">.</span><span class="token function">CryptBlocks</span><span class="token punctuation">(</span>dst<span class="token punctuation">,</span> src<span class="token punctuation">)</span>

	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;加密之后的数据: &quot;</span><span class="token punctuation">,</span> dst<span class="token punctuation">)</span>

	<span class="token comment">// 6. 将加密数据返回</span>
	<span class="token keyword">return</span> dst
<span class="token punctuation">}</span>
</code></pre></div><p>DES解密代码</p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token comment">// src -&gt; 要解密的密文</span>
<span class="token comment">// key -&gt; 秘钥, 和加密秘钥相同, 大小为: 8byte</span>
<span class="token keyword">func</span> <span class="token function">DesDecrypt_CBC</span><span class="token punctuation">(</span>src<span class="token punctuation">,</span> key <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span> <span class="token punctuation">{</span>
	<span class="token comment">// 1. 创建并返回一个使用DES算法的cipher.Block接口</span>
	block<span class="token punctuation">,</span> err <span class="token operator">:=</span> des<span class="token punctuation">.</span><span class="token function">NewCipher</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span>
	<span class="token comment">// 2. 判断是否创建成功</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span><span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// 3. 创建一个密码分组为链接模式的, 底层使用DES解密的BlockMode接口</span>
	tmp <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token function">byte</span><span class="token punctuation">(</span><span class="token string">&quot;helloAAA&quot;</span><span class="token punctuation">)</span>
	blockMode <span class="token operator">:=</span> cipher<span class="token punctuation">.</span><span class="token function">NewCBCDecrypter</span><span class="token punctuation">(</span>block<span class="token punctuation">,</span> tmp<span class="token punctuation">)</span>
	<span class="token comment">// 4. 解密数据</span>
	dst <span class="token operator">:=</span> src
	blockMode<span class="token punctuation">.</span><span class="token function">CryptBlocks</span><span class="token punctuation">(</span>src<span class="token punctuation">,</span> dst<span class="token punctuation">)</span>
	<span class="token comment">// 5. 去掉最后一组填充的数据</span>
	dst <span class="token operator">=</span> <span class="token function">PKCS5UnPadding</span><span class="token punctuation">(</span>dst<span class="token punctuation">)</span>

	<span class="token comment">// 6. 返回结果</span>
	<span class="token keyword">return</span> dst
<span class="token punctuation">}</span>
</code></pre></div><p>最后一个分组添加填充数据和移除添加数据代码</p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token comment">// 使用pks5的方式填充</span>
<span class="token keyword">func</span> <span class="token function">PKCS5Padding</span><span class="token punctuation">(</span>ciphertext <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">,</span> blockSize <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">{</span>
	<span class="token comment">// 1. 计算最后一个分组缺多少个字节</span>
	padding <span class="token operator">:=</span> blockSize <span class="token operator">-</span> <span class="token punctuation">(</span><span class="token function">len</span><span class="token punctuation">(</span>ciphertext<span class="token punctuation">)</span><span class="token operator">%</span>blockSize<span class="token punctuation">)</span>
	<span class="token comment">// 2. 创建一个大小为padding的切片, 每个字节的值为padding</span>
	padText <span class="token operator">:=</span> bytes<span class="token punctuation">.</span><span class="token function">Repeat</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">{</span><span class="token function">byte</span><span class="token punctuation">(</span>padding<span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">,</span> padding<span class="token punctuation">)</span>
	<span class="token comment">// 3. 将padText添加到原始数据的后边, 将最后一个分组缺少的字节数补齐</span>
	newText <span class="token operator">:=</span> <span class="token function">append</span><span class="token punctuation">(</span>ciphertext<span class="token punctuation">,</span> padText<span class="token operator">...</span><span class="token punctuation">)</span>
	<span class="token keyword">return</span> newText
<span class="token punctuation">}</span>

<span class="token comment">// 删除pks5填充的尾部数据</span>
<span class="token keyword">func</span> <span class="token function">PKCS5UnPadding</span><span class="token punctuation">(</span>origData <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">{</span>
	<span class="token comment">// 1. 计算数据的总长度</span>
	length <span class="token operator">:=</span> <span class="token function">len</span><span class="token punctuation">(</span>origData<span class="token punctuation">)</span>
	<span class="token comment">// 2. 根据填充的字节值得到填充的次数</span>
	number <span class="token operator">:=</span> <span class="token function">int</span><span class="token punctuation">(</span>origData<span class="token punctuation">[</span>length<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
	<span class="token comment">// 3. 将尾部填充的number个字节去掉</span>
	<span class="token keyword">return</span> origData<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">(</span>length<span class="token operator">-</span>number<span class="token punctuation">)</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre></div><p>测试函数</p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">DESText</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">// 加密</span>
	key <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token function">byte</span><span class="token punctuation">(</span><span class="token string">&quot;11111111&quot;</span><span class="token punctuation">)</span>
	result <span class="token operator">:=</span> <span class="token function">DesEncrypt_CBC</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token function">byte</span><span class="token punctuation">(</span><span class="token string">&quot;床前明月光, 疑是地上霜. 举头望明月, 低头思故乡.&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span> key<span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>base64<span class="token punctuation">.</span>StdEncoding<span class="token punctuation">.</span><span class="token function">EncodeToString</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span><span class="token punctuation">)</span>
	<span class="token comment">// 解密</span>
	result <span class="token operator">=</span> <span class="token function">DesDecrypt_CBC</span><span class="token punctuation">(</span>result<span class="token punctuation">,</span> key<span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;解密之后的数据: &quot;</span><span class="token punctuation">,</span> <span class="token function">string</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div><p>重要的函数说明</p> <ol><li><p>生成一个底层使用DES加/解密的Block接口对象</p> <div class="language-go extra-class"><pre class="language-go"><code>函数对应的包<span class="token punctuation">:</span> <span class="token keyword">import</span> <span class="token string">&quot;crypto/des&quot;</span>
<span class="token keyword">func</span> <span class="token function">NewCipher</span><span class="token punctuation">(</span>key <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>cipher<span class="token punctuation">.</span>Block<span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>
	<span class="token operator">-</span> 参数 key<span class="token punctuation">:</span> des对称加密使用的密码<span class="token punctuation">,</span> 密码长度为<span class="token number">64</span>bit<span class="token punctuation">,</span> 即<span class="token number">8</span><span class="token builtin">byte</span>
	<span class="token operator">-</span> 返回值 cipher<span class="token punctuation">.</span>Block<span class="token punctuation">:</span> 创建出的使用DES加<span class="token operator">/</span>解密的Block接口对象
</code></pre></div></li> <li><p>创建一个密码分组为CBC模式, 底层使用b加密的BlockMode接口对象</p> <div class="language-go extra-class"><pre class="language-go"><code>函数对应的包<span class="token punctuation">:</span> <span class="token keyword">import</span> <span class="token string">&quot;crypto/cipher&quot;</span>
<span class="token keyword">func</span> <span class="token function">NewCBCEncrypter</span><span class="token punctuation">(</span>b Block<span class="token punctuation">,</span> iv <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> BlockMode
    <span class="token operator">-</span> 参数 b<span class="token punctuation">:</span> 使用des<span class="token punctuation">.</span>NewCipher函数创建出的Block接口对象
    <span class="token operator">-</span> 参数 iv<span class="token punctuation">:</span> 事先准备好的一个长度为一个分组长度的比特序列<span class="token punctuation">,</span> 每个分组为<span class="token number">64</span>bit<span class="token punctuation">,</span> 即<span class="token number">8</span><span class="token builtin">byte</span>
    <span class="token operator">-</span> 返回值<span class="token punctuation">:</span> 得到的BlockMode接口对象
</code></pre></div></li> <li><p>使用cipher包的BlockMode接口对象对数据进行加/解密</p> <div class="language-go extra-class"><pre class="language-go"><code>接口对应的包<span class="token punctuation">:</span> <span class="token keyword">import</span> <span class="token string">&quot;crypto/cipher&quot;</span>
<span class="token keyword">type</span> BlockMode <span class="token keyword">interface</span> <span class="token punctuation">{</span>
    <span class="token comment">// 返回加密字节块的大小</span>
    <span class="token function">BlockSize</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">int</span>
    <span class="token comment">// 加密或解密连续的数据块，src的尺寸必须是块大小的整数倍，src和dst可指向同一内存地址</span>
    <span class="token function">CryptBlocks</span><span class="token punctuation">(</span>dst<span class="token punctuation">,</span> src <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
接口中的 <span class="token function">CryptBlocks</span><span class="token punctuation">(</span>dst<span class="token punctuation">,</span> src <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> 方法<span class="token punctuation">:</span>
    <span class="token operator">-</span> 参数 dst<span class="token punctuation">:</span> 传出参数<span class="token punctuation">,</span> 存储加密或解密运算之后的结果 
    <span class="token operator">-</span> 参数 src<span class="token punctuation">:</span> 传入参数<span class="token punctuation">,</span> 需要进行加密或解密的数据切片<span class="token punctuation">(</span>字符串<span class="token punctuation">)</span>
</code></pre></div></li> <li><p>创建一个密码分组为CBC模式, 底层使用b解密的BlockMode接口对象</p> <div class="language-go extra-class"><pre class="language-go"><code>函数对应的包<span class="token punctuation">:</span> <span class="token keyword">import</span> <span class="token string">&quot;crypto/cipher&quot;</span>
<span class="token keyword">func</span> <span class="token function">NewCBCDecrypter</span><span class="token punctuation">(</span>b Block<span class="token punctuation">,</span> iv <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> BlockMode
    <span class="token operator">-</span> 参数 b<span class="token punctuation">:</span> 使用des<span class="token punctuation">.</span>NewCipher函数创建出的Block接口对象
    <span class="token operator">-</span> 参数 iv<span class="token punctuation">:</span> 事先准备好的一个长度为一个分组长度的比特序列<span class="token punctuation">,</span> 每个分组为<span class="token number">64</span>bit<span class="token punctuation">,</span> 即<span class="token number">8</span><span class="token builtin">byte</span><span class="token punctuation">,</span> 
               该序列的值需要和NewCBCEncrypter函数的第二个参数iv值相同
    <span class="token operator">-</span> 返回值<span class="token punctuation">:</span> 得到的BlockMode接口对象
</code></pre></div></li> <li><p>自定义函数介绍</p> <div class="language-go extra-class"><pre class="language-go"><code>对称加密加密需要对数据进行分组<span class="token punctuation">,</span> 保证每个分组的数据长度相等<span class="token punctuation">,</span> 如果最后一个分组长度不够<span class="token punctuation">,</span> 需要进行填充
<span class="token keyword">func</span> <span class="token function">PKCS5Padding</span><span class="token punctuation">(</span>ciphertext <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">,</span> blockSize <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span>
    <span class="token operator">-</span> 参数 ciphertext<span class="token punctuation">:</span> 需要加密的原始数据
    <span class="token operator">-</span> 参数 blockSize<span class="token punctuation">:</span> 每个分组的长度<span class="token punctuation">,</span> 跟使用的加密算法有关系
		<span class="token operator">*</span> des：<span class="token number">64</span>bit， <span class="token number">8</span><span class="token builtin">byte</span>
		<span class="token operator">*</span> <span class="token number">3</span>des：<span class="token number">64</span>bit， <span class="token number">8</span><span class="token builtin">byte</span>
		<span class="token operator">*</span> aes： <span class="token number">128</span>bit， <span class="token number">16</span><span class="token builtin">byte</span>
</code></pre></div></li></ol> <h3 id="_2-3-三重des"><a href="#_2-3-三重des" class="header-anchor">#</a> 2.3 三重DES</h3> <blockquote><p>现在DES已经可以在现实的时间内被暴力破解，因此我们需要一种用来替代DES的分组密码，三重DES就是出于这个目的被开发出来的。</p> <p><strong>三重DES（triple-DES）是为了增加DES的强度，==将DES重复3次所得到的一种密码算法==，通常缩写为3DES</strong>。</p></blockquote> <h4 id="_2-3-1-三重des的加密"><a href="#_2-3-1-三重des的加密" class="header-anchor">#</a> 2.3.1 三重DES的加密</h4> <blockquote><p>三重DES的加解密机制如图所示：</p> <p>加-&gt;解-&gt;加 -&gt; 目的是为了兼容des</p> <p>3des秘钥长度24字节 = 1234567a 1234567b 1234567a</p> <p>明文: 10</p> <p>秘钥1: 2</p> <p>秘钥2: 3</p> <p>秘钥3: 4</p> <p>加密算法: 明文+秘钥</p> <p>解密算法: 密文-秘钥</p> <p>10+2-3+4</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538883542015.png" alt="1538883542015"></p> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538730367361.png" alt="1538730367361"></p> <blockquote><p>明文经过三次DES处理才能变成最后的密文，由于<strong>DES密钥的长度实质上是56比特</strong>，因此<font color="red">三重DES的密钥长度就是56×3=168比特, 加上用于错误检测的标志位8x3, 共192bit</font>。</p> <p>从上图我们可以发现，三重DES并不是进行三次DES加密（加密--&gt;加密--&gt;加密），而是<font color="red"><strong>加密--&gt;解密--&gt;加密</strong></font>的过程。在加密算法中加人解密操作让人感觉很不可思议，实际上这个方法是IBM公司设计出来的，目的是为了让三重DES能够兼容普通的DES。</p> <p><font color="red">当三重DES中所有的密钥都相同时，三重DES也就等同于普通的DES了</font>。这是因为在前两步加密--&gt;解密之后，得到的就是最初的明文。因此，以前用DES加密的密文，就可以通过这种方式用三重DES来进行解密。也就是说，三重DES对DES具备向下兼容性。</p> <p>如果密钥1和密钥3使用相同的密钥，而密钥2使用不同的密钥（也就是只使用两个DES密钥），这种三重DES就称为DES-EDE2。EDE表示的是加密（Encryption) --&gt;解密（Decryption)--&gt;加密（Encryption）这个流程。</p> <p>密钥1、密钥2、密钥3全部使用不同的比特序列的三重DES称为DES-EDE3。</p></blockquote> <blockquote><p>尽管三重DES目前还被银行等机构使用，但其处理速度不高，而且在安全性方面也逐渐显现出了一些问题。</p></blockquote> <h4 id="_2-3-2-go中对3des的操作"><a href="#_2-3-2-go中对3des的操作" class="header-anchor">#</a> 2.3.2 Go中对3DES的操作</h4> <h5 id="加解密实现思路-2"><a href="#加解密实现思路-2" class="header-anchor">#</a> 加解密实现思路</h5> <ul><li><p><strong>加密 - CBC分组模式</strong></p> <blockquote><ol><li>创建并返回一个使用3DES算法的cipher.Block接口
<ul><li><strong>秘钥长度为64bit*3=192bit, 即 192/8 = 24字节(byte)</strong></li></ul></li> <li>对最后一个明文分组进行数据填充
<ul><li>3DES是以64比特的明文（比特序列）为一个单位来进行加密的</li> <li>最后一组不够64bit, 则需要进行数据填充( <strong>参考第三章</strong>)</li></ul></li> <li>创建一个密码分组为链接模式的, 底层使用3DES加密的BlockMode接口</li> <li>加密连续的数据块</li></ol></blockquote></li> <li><p><strong>解密</strong></p> <blockquote><ol><li>创建并返回一个使用3DES算法的cipher.Block接口</li> <li>创建一个密码分组为链接模式的, 底层使用3DES解密的BlockMode接口</li> <li>数据块解密</li> <li>去掉最后一组的填充数据</li></ol></blockquote></li></ul> <h5 id="加解密的代码实现-2"><a href="#加解密的代码实现-2" class="header-anchor">#</a> 加解密的代码实现</h5> <p>3DES加密代码</p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token comment">// 3DES加密</span>
<span class="token keyword">func</span> <span class="token function">TripleDESEncrypt</span><span class="token punctuation">(</span>src<span class="token punctuation">,</span> key <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span> <span class="token punctuation">{</span>
	<span class="token comment">// 1. 创建并返回一个使用3DES算法的cipher.Block接口</span>
	block<span class="token punctuation">,</span> err <span class="token operator">:=</span> des<span class="token punctuation">.</span><span class="token function">NewTripleDESCipher</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span><span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// 2. 对最后一组明文进行填充</span>
	src <span class="token operator">=</span> <span class="token function">PKCS5Padding</span><span class="token punctuation">(</span>src<span class="token punctuation">,</span> block<span class="token punctuation">.</span><span class="token function">BlockSize</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
	<span class="token comment">// 3. 创建一个密码分组为链接模式, 底层使用3DES加密的BlockMode模型</span>
	blockMode <span class="token operator">:=</span> cipher<span class="token punctuation">.</span><span class="token function">NewCBCEncrypter</span><span class="token punctuation">(</span>block<span class="token punctuation">,</span> key<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
	<span class="token comment">// 4. 加密数据</span>
	dst <span class="token operator">:=</span> src
	blockMode<span class="token punctuation">.</span><span class="token function">CryptBlocks</span><span class="token punctuation">(</span>dst<span class="token punctuation">,</span> src<span class="token punctuation">)</span>
	<span class="token keyword">return</span> dst
<span class="token punctuation">}</span>
</code></pre></div><p>3DES解密代码</p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token comment">// 3DES解密</span>
<span class="token keyword">func</span> <span class="token function">TripleDESDecrypt</span><span class="token punctuation">(</span>src<span class="token punctuation">,</span> key <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span> <span class="token punctuation">{</span>
	<span class="token comment">// 1. 创建3DES算法的Block接口对象</span>
	block<span class="token punctuation">,</span> err <span class="token operator">:=</span> des<span class="token punctuation">.</span><span class="token function">NewTripleDESCipher</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span><span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// 2. 创建密码分组为链接模式, 底层使用3DES解密的BlockMode模型</span>
	blockMode <span class="token operator">:=</span> cipher<span class="token punctuation">.</span><span class="token function">NewCBCDecrypter</span><span class="token punctuation">(</span>block<span class="token punctuation">,</span> key<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
	<span class="token comment">// 3. 解密</span>
	dst <span class="token operator">:=</span> src
	blockMode<span class="token punctuation">.</span><span class="token function">CryptBlocks</span><span class="token punctuation">(</span>dst<span class="token punctuation">,</span> src<span class="token punctuation">)</span>
	<span class="token comment">// 4. 去掉尾部填充的数据</span>
	dst <span class="token operator">=</span> <span class="token function">PKCS5UnPadding</span><span class="token punctuation">(</span>dst<span class="token punctuation">)</span>
	<span class="token keyword">return</span> dst
<span class="token punctuation">}</span>
</code></pre></div><p>重要的函数说明</p> <ol><li><p>生成一个底层使用3DES加/解密的Block接口对象</p> <div class="language-go extra-class"><pre class="language-go"><code>函数对应的包<span class="token punctuation">:</span> <span class="token keyword">import</span> <span class="token string">&quot;crypto/des&quot;</span>
<span class="token keyword">func</span> <span class="token function">NewTripleDESCipher</span><span class="token punctuation">(</span>key <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>cipher<span class="token punctuation">.</span>Block<span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>
	<span class="token operator">-</span> 参数 key<span class="token punctuation">:</span> <span class="token number">3</span>des对称加密使用的密码<span class="token punctuation">,</span> 密码长度为<span class="token punctuation">(</span><span class="token number">64</span><span class="token operator">*</span><span class="token number">3</span><span class="token punctuation">)</span>bit<span class="token punctuation">,</span> 即<span class="token punctuation">(</span><span class="token number">8</span><span class="token operator">*</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token builtin">byte</span>
	<span class="token operator">-</span> 返回值 cipher<span class="token punctuation">.</span>Block<span class="token punctuation">:</span> 创建出的使用DES加<span class="token operator">/</span>解密的Block接口对象
</code></pre></div></li> <li><p>创建一个密码分组为CBC模式, 底层使用b加密的BlockMode接口对象</p> <div class="language-go extra-class"><pre class="language-go"><code>函数对应的包<span class="token punctuation">:</span> <span class="token keyword">import</span> <span class="token string">&quot;crypto/cipher&quot;</span>
<span class="token keyword">func</span> <span class="token function">NewCBCEncrypter</span><span class="token punctuation">(</span>b Block<span class="token punctuation">,</span> iv <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> BlockMode
    <span class="token operator">-</span> 参数 b<span class="token punctuation">:</span> 使用des<span class="token punctuation">.</span>NewTripleDESCipher 函数创建出的Block接口对象
    <span class="token operator">-</span> 参数 iv<span class="token punctuation">:</span> 事先准备好的一个长度为一个分组长度的比特序列<span class="token punctuation">,</span> 每个分组为<span class="token number">64</span>bit<span class="token punctuation">,</span> 即<span class="token number">8</span><span class="token builtin">byte</span>
    <span class="token operator">-</span> 返回值<span class="token punctuation">:</span> 得到的BlockMode接口对象
</code></pre></div></li> <li><p>使用cipher包的BlockMode接口对象对数据进行加/解密</p> <div class="language-go extra-class"><pre class="language-go"><code>接口对应的包<span class="token punctuation">:</span> <span class="token keyword">import</span> <span class="token string">&quot;crypto/cipher&quot;</span>
<span class="token keyword">type</span> BlockMode <span class="token keyword">interface</span> <span class="token punctuation">{</span>
    <span class="token comment">// 返回加密字节块的大小</span>
    <span class="token function">BlockSize</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">int</span>
    <span class="token comment">// 加密或解密连续的数据块，src的尺寸必须是块大小的整数倍，src和dst可指向同一内存地址</span>
    <span class="token function">CryptBlocks</span><span class="token punctuation">(</span>dst<span class="token punctuation">,</span> src <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
接口中的 <span class="token function">CryptBlocks</span><span class="token punctuation">(</span>dst<span class="token punctuation">,</span> src <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> 方法<span class="token punctuation">:</span>
    <span class="token operator">-</span> 参数 dst<span class="token punctuation">:</span> 传出参数<span class="token punctuation">,</span> 存储加密或解密运算之后的结果 
    <span class="token operator">-</span> 参数 src<span class="token punctuation">:</span> 传入参数<span class="token punctuation">,</span> 需要进行加密或解密的数据切片<span class="token punctuation">(</span>字符串<span class="token punctuation">)</span>
</code></pre></div></li> <li><p>创建一个密码分组为CBC模式, 底层使用b解密的BlockMode接口对象</p> <div class="language-go extra-class"><pre class="language-go"><code>函数对应的包<span class="token punctuation">:</span> <span class="token keyword">import</span> <span class="token string">&quot;crypto/cipher&quot;</span>
<span class="token keyword">func</span> <span class="token function">NewCBCDecrypter</span><span class="token punctuation">(</span>b Block<span class="token punctuation">,</span> iv <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> BlockMode
    <span class="token operator">-</span> 参数 b<span class="token punctuation">:</span> 使用des<span class="token punctuation">.</span>NewTripleDESCipher 函数创建出的Block接口对象
    <span class="token operator">-</span> 参数 iv<span class="token punctuation">:</span> 事先准备好的一个长度为一个分组长度的比特序列<span class="token punctuation">,</span> 每个分组为<span class="token number">64</span>bit<span class="token punctuation">,</span> 即<span class="token number">8</span><span class="token builtin">byte</span><span class="token punctuation">,</span> 
               该序列的值需要和NewCBCEncrypter函数的第二个参数iv值相同
    <span class="token operator">-</span> 返回值<span class="token punctuation">:</span> 得到的BlockMode接口对象
</code></pre></div></li></ol> <h3 id="_2-4-aes"><a href="#_2-4-aes" class="header-anchor">#</a> 2.4 AES</h3> <blockquote><p>AES（Advanced Encryption Standard）是取代其前任标准（DES）而成为新标准的一种对称密码算法。全世界的企业和密码学家提交了多个对称密码算法作为AES的候选，最终在2000年从这些候选算法中选出了一种名为==<strong>Rijndael</strong>==的对称密码算法，并将其确定为了AES。</p> <p>Rijndael是由比利时密码学家Joan Daemen和Vincent Rijmen设汁的分组密码算法，今后会有越来越多的密码软件支持这种算法。</p> <p><strong>==Rijndael的分组长度为128比特==</strong>，密钥长度可以以32比特为单位在128比特到256比特的范围内进行选择（不过==<strong>在AES的规格中，密钥长度只有128、192和256比特三种</strong>==）。</p> <p>128bit = 16字节</p> <p>192bit = 24字节</p> <p>256bit = 32字节</p> <p>在go提供的接口中秘钥长度只能是16字节</p></blockquote> <h4 id="_2-4-2-aes的加密和解密"><a href="#_2-4-2-aes的加密和解密" class="header-anchor">#</a> 2.4.2 AES的加密和解密</h4> <blockquote><p>和DES—样，AES算法也是由多个轮所构成的，下图展示了每一轮的大致计算步骤。DES使用Feistel网络作为其基本结构，而AES没有使用Feistel网络，而是使用了SPN Rijndael的输人分组为128比特，也就是16字节。首先，需要逐个字节地对16字节的输入数据进行SubBytes处理。所谓SubBytes,就是以每个字节的值（0～255中的任意值）为索引，从一张拥有256个值的替换表（S-Box）中查找出对应值的处理，也是说，将一个1字节的值替换成另一个1字节的值。</p> <p>SubBytes之后需要进行ShiftRows处理，即将SubBytes的输出以字节为单位进行打乱处理。从下图的线我们可以看出，这种打乱处理是有规律的。</p> <p>ShiftRows之后需要进行MixCo1umns处理，即对一个4字节的值进行比特运算，将其变为另外一个4字节值。</p> <p>最后，需要将MixColumns的输出与轮密钥进行XOR，即进行AddRoundKey处理。到这里，AES的一轮就结東了。实际上，在AES中需要重复进行10 ~ 14轮计算。</p> <p>通过上面的结构我们可以发现输入的所有比特在一轮中都会被加密。和每一轮都只加密一半输人的比特的Feistel网络相比，这种方式的优势在于加密所需要的轮数更少。此外，这种方式还有一个优势，即SubBytes，ShiftRows和MixColumns可以分别按字节、行和列为单位进行并行计算。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538731104755.png" alt="1538731104755"></p> <blockquote><p><font color="red">SubBytes		-- 	字节代换</font></p> <p><font color="red">ShiftRows		--	行移位代换</font></p> <p><font color="red">MixColumns	--	列混淆	</font></p> <p><font color="red">AddRoundKey	--	轮密钥加</font></p></blockquote> <blockquote><p>下图展示了AES中一轮的解密过程。从图中我们可以看出，SubBytes、ShiftRows、MixColumns分别存在反向运算InvSubBytes、InvShiftRows、InvMixColumns，这是因为AES不像Feistel网络一样能够用同一种结构实现加密和解密。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538731285324.png" alt="1538731285324"></p> <blockquote><p><font color="red">InvSubBytes		--	逆字节替代</font></p> <p><font color="red">InvShiftRows		--	逆行移位</font></p> <p><font color="red">InvMixColumns	--	逆列混淆</font></p></blockquote> <h4 id="_2-4-2-go中对aes的使用"><a href="#_2-4-2-go中对aes的使用" class="header-anchor">#</a> 2.4.2 Go中对AES的使用</h4> <h5 id="加解密实现思路-3"><a href="#加解密实现思路-3" class="header-anchor">#</a> 加解密实现思路</h5> <ul><li><p><strong>加密 - CBC分组模式</strong></p> <blockquote><ol><li>创建并返回一个使用AES算法的cipher.Block接口
<ul><li><strong>秘钥长度为128bit, 即 128/8 = 16字节(byte)</strong></li></ul></li> <li>对最后一个明文分组进行数据填充
<ul><li>AES是以128比特的明文（比特序列）为一个单位来进行加密的</li> <li>最后一组不够128bit, 则需要进行数据填充( <strong>参考第三章</strong>)</li></ul></li> <li>创建一个密码分组为链接模式的, 底层使用AES加密的BlockMode接口</li> <li>加密连续的数据块</li></ol></blockquote></li> <li><p><strong>解密</strong></p> <blockquote><ol><li>创建并返回一个使用AES算法的cipher.Block接口</li> <li>创建一个密码分组为链接模式的, 底层使用AES解密的BlockMode接口</li> <li>数据块解密</li> <li>去掉最后一组的填充数据</li></ol></blockquote></li></ul> <h5 id="加解密的代码实现-3"><a href="#加解密的代码实现-3" class="header-anchor">#</a> 加解密的代码实现</h5> <p>AES加密代码</p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token comment">// AES加密</span>
<span class="token keyword">func</span> <span class="token function">AESEncrypt</span><span class="token punctuation">(</span>src<span class="token punctuation">,</span> key <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">{</span>
	<span class="token comment">// 1. 创建一个使用AES加密的块对象</span>
	block<span class="token punctuation">,</span> err <span class="token operator">:=</span> aes<span class="token punctuation">.</span><span class="token function">NewCipher</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span><span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// 2. 最后一个分组进行数据填充</span>
	src <span class="token operator">=</span> <span class="token function">PKCS5Padding</span><span class="token punctuation">(</span>src<span class="token punctuation">,</span> block<span class="token punctuation">.</span><span class="token function">BlockSize</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
	<span class="token comment">// 3. 创建一个分组为链接模式, 底层使用AES加密的块模型对象</span>
	blockMode <span class="token operator">:=</span> cipher<span class="token punctuation">.</span><span class="token function">NewCBCEncrypter</span><span class="token punctuation">(</span>block<span class="token punctuation">,</span> key<span class="token punctuation">[</span><span class="token punctuation">:</span>block<span class="token punctuation">.</span><span class="token function">BlockSize</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
	<span class="token comment">// 4. 加密</span>
	dst <span class="token operator">:=</span> src
	blockMode<span class="token punctuation">.</span><span class="token function">CryptBlocks</span><span class="token punctuation">(</span>dst<span class="token punctuation">,</span> src<span class="token punctuation">)</span>
	<span class="token keyword">return</span> dst
<span class="token punctuation">}</span>
</code></pre></div><p>AES解密</p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token comment">// AES解密</span>
<span class="token keyword">func</span> <span class="token function">AESDecrypt</span><span class="token punctuation">(</span>src<span class="token punctuation">,</span> key <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">{</span>
	<span class="token comment">// 1. 创建一个使用AES解密的块对象</span>
	block<span class="token punctuation">,</span> err <span class="token operator">:=</span> aes<span class="token punctuation">.</span><span class="token function">NewCipher</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span><span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// 2. 创建分组为链接模式, 底层使用AES的解密模型对象</span>
	blockMode <span class="token operator">:=</span> cipher<span class="token punctuation">.</span><span class="token function">NewCBCDecrypter</span><span class="token punctuation">(</span>block<span class="token punctuation">,</span> key<span class="token punctuation">[</span><span class="token punctuation">:</span>block<span class="token punctuation">.</span><span class="token function">BlockSize</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
	<span class="token comment">// 3. 解密</span>
	dst <span class="token operator">:=</span> src
	blockMode<span class="token punctuation">.</span><span class="token function">CryptBlocks</span><span class="token punctuation">(</span>dst<span class="token punctuation">,</span> src<span class="token punctuation">)</span>
	<span class="token comment">// 4. 去掉尾部填充的字</span>
	dst <span class="token operator">=</span> <span class="token function">PKCS5UnPadding</span><span class="token punctuation">(</span>dst<span class="token punctuation">)</span>
	<span class="token keyword">return</span> dst
<span class="token punctuation">}</span>
</code></pre></div><p>重要的函数说明</p> <ol><li><p>生成一个底层使用AES加/解密的Block接口对象</p> <div class="language-go extra-class"><pre class="language-go"><code>函数对应的包<span class="token punctuation">:</span> <span class="token keyword">import</span> <span class="token string">&quot;crypto/aes&quot;</span>
<span class="token keyword">func</span> <span class="token function">NewCipher</span><span class="token punctuation">(</span>key <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>cipher<span class="token punctuation">.</span>Block<span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>
	<span class="token operator">-</span> 参数 key<span class="token punctuation">:</span> aes对称加密使用的密码<span class="token punctuation">,</span> 密码长度为<span class="token number">128</span>bit<span class="token punctuation">,</span> 即<span class="token number">16</span><span class="token builtin">byte</span>
	<span class="token operator">-</span> 返回值 cipher<span class="token punctuation">.</span>Block<span class="token punctuation">:</span> 创建出的使用AES加<span class="token operator">/</span>解密的Block接口对象
</code></pre></div></li> <li><p>创建一个密码分组为CBC模式, 底层使用b加密的BlockMode接口对象</p> <div class="language-go extra-class"><pre class="language-go"><code>函数对应的包<span class="token punctuation">:</span> <span class="token keyword">import</span> <span class="token string">&quot;crypto/cipher&quot;</span>
<span class="token keyword">func</span> <span class="token function">NewCBCEncrypter</span><span class="token punctuation">(</span>b Block<span class="token punctuation">,</span> iv <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> BlockMode
    <span class="token operator">-</span> 参数 b<span class="token punctuation">:</span> 使用aes<span class="token punctuation">.</span>NewCipher函数创建出的Block接口对象
    <span class="token operator">-</span> 参数 iv<span class="token punctuation">:</span> 事先准备好的一个长度为一个分组长度的比特序列<span class="token punctuation">,</span> 每个分组为<span class="token number">64</span>bit<span class="token punctuation">,</span> 即<span class="token number">8</span><span class="token builtin">byte</span>
    <span class="token operator">-</span> 返回值<span class="token punctuation">:</span> 得到的BlockMode接口对象
</code></pre></div></li> <li><p>使用cipher包的BlockMode接口对象对数据进行加/解密</p> <div class="language-go extra-class"><pre class="language-go"><code>接口对应的包<span class="token punctuation">:</span> <span class="token keyword">import</span> <span class="token string">&quot;crypto/cipher&quot;</span>
<span class="token keyword">type</span> BlockMode <span class="token keyword">interface</span> <span class="token punctuation">{</span>
    <span class="token comment">// 返回加密字节块的大小</span>
    <span class="token function">BlockSize</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">int</span>
    <span class="token comment">// 加密或解密连续的数据块，src的尺寸必须是块大小的整数倍，src和dst可指向同一内存地址</span>
    <span class="token function">CryptBlocks</span><span class="token punctuation">(</span>dst<span class="token punctuation">,</span> src <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
接口中的 <span class="token function">CryptBlocks</span><span class="token punctuation">(</span>dst<span class="token punctuation">,</span> src <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> 方法<span class="token punctuation">:</span>
    <span class="token operator">-</span> 参数 dst<span class="token punctuation">:</span> 传出参数<span class="token punctuation">,</span> 存储加密或解密运算之后的结果 
    <span class="token operator">-</span> 参数 src<span class="token punctuation">:</span> 传入参数<span class="token punctuation">,</span> 需要进行加密或解密的数据切片<span class="token punctuation">(</span>字符串<span class="token punctuation">)</span>
</code></pre></div></li> <li><p>创建一个密码分组为CBC模式, 底层使用b解密的BlockMode接口对象</p> <div class="language-go extra-class"><pre class="language-go"><code>函数对应的包<span class="token punctuation">:</span> <span class="token keyword">import</span> <span class="token string">&quot;crypto/cipher&quot;</span>
<span class="token keyword">func</span> <span class="token function">NewCBCDecrypter</span><span class="token punctuation">(</span>b Block<span class="token punctuation">,</span> iv <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> BlockMode
    <span class="token operator">-</span> 参数 b<span class="token punctuation">:</span> 使用des<span class="token punctuation">.</span>NewCipher函数创建出的Block接口对象
    <span class="token operator">-</span> 参数 iv<span class="token punctuation">:</span> 事先准备好的一个长度为一个分组长度的比特序列<span class="token punctuation">,</span> 每个分组为<span class="token number">128</span>bit<span class="token punctuation">,</span> 即<span class="token number">16</span><span class="token builtin">byte</span><span class="token punctuation">,</span> 
               该序列的值需要和NewCBCEncrypter函数的第二个参数iv值相同
    <span class="token operator">-</span> 返回值<span class="token punctuation">:</span> 得到的BlockMode接口对象
</code></pre></div></li></ol> <h3 id="_2-5-应选择哪种对称加密"><a href="#_2-5-应选择哪种对称加密" class="header-anchor">#</a> 2.5 应选择哪种对称加密</h3> <blockquote><p>前面我们介绍了DES、三重DES和AES等对称密码，那么我们到底应该使用哪一种对称密码算法呢？</p> <ol><li><strong>今后最好不要将DES用于新的用途，因为随着计算机技术的进步，现在用暴力破解法已经能够在现实的时间内完成对DES的破译。但是，在某些情况下也需要保持与旧版本软件的兼容性。</strong></li> <li><strong>出于兼容性的因素三重DES在今后还会使用一段时间，但会逐渐被AES所取代。</strong></li> <li><strong>今后大家应该使用的算法是AES（Rijndael），因为它安全、快速，而且能够在各种平台上工作。此外，由于全世界的密码学家都在对AES进行不断的验证，因此即便万一发现它有什么缺陷，也会立刻告知全世界并修复这些缺陷。</strong></li></ol> <p>一般来说，我们不应该使用任何自制的密码算法，而是应该使用AES。因为AES在其选定过程中，经过了全世界密码学家所进行的高品质的验证工作，而对于自制的密码算法则很难进行这样的验证。</p></blockquote> <h4 id="本章小结"><a href="#本章小结" class="header-anchor">#</a> 本章小结</h4> <blockquote><p>本章中我们介绍了对称密码，以及DES、三重DES、AES和其他一些密码算法。</p> <p>使用一种密钥空间巨大，且在算法上没有弱点的对称密码，就可以通过密文来确保明文的机密性。巨大的密钥空间能够抵御暴力破解，算法上没有弱点可以抵御其他类型的攻击。</p> <p>然而，用对称密码进行通信时，还会出现密钥的配送问题，即如何将密钥安全地发送给接收者。为了解决密钥配送问题，我们需要非对称加密技术。非对称加密，我们将在第四章进行讲解。</p> <p>本章所介绍的几乎所有的密码算法，都只能将一个固定长度的分组进行加密当需要加密的明文长度超过分组长度时，就需要对密码算法进行迭代下一章我们将探讨对分组密码进行迭代的方法。</p></blockquote> <h2 id="_3-分组密码的模式"><a href="#_3-分组密码的模式" class="header-anchor">#</a> 3. 分组密码的模式</h2> <div class="language-go extra-class"><pre class="language-go"><code><span class="token string">&quot;分组密码的模式 -- 分组密码是如何迭代的&quot;</span>
</code></pre></div><blockquote><p>本章中我们将探讨一下分组密码的模式</p> <p>我们在上一章中介绍的DES和AES都属于分组密码，它们只能加密固定长度的明文。如果需要加密任意长度的明文，就需要对分组密码进行迭代，而分组密码的迭代方法就称为分组密码的“模式”。</p> <p>分组密码有很多种模式，如果模式的选择不恰当，就无法保证机密性。例如，如果使用ECB模式，明文中的一些规律就可以通过密文被识别出来。</p> <p>分组密码的主要模式（ECB、CBC、CFB、OFB、CTR），最后再来考察一下到底应该使用哪一种模式。</p></blockquote> <h3 id="_3-1-分组密码"><a href="#_3-1-分组密码" class="header-anchor">#</a> 3.1 分组密码</h3> <blockquote><p>**分组密码（blockcipher）**是每次只能处理特定长度的一块数据的一类密码算法，这里的一块&quot;就称为分组（block）。此外，一个分组的比特数就称为分组长度（blocklength）。</p> <p>例如，<strong>DES和三重DES的分组长度都是64比特</strong>。这些密码算法一次只能加密64比特的明文．并生成64比特的密文。</p> <p><strong>AES的分组长度可以从128比特、192比特和256比特中进行选择。当选择128比特的分组长度时，AES一次可加密128比特的明文，并生成128比特的密文。</strong></p></blockquote> <h3 id="_3-2-模式"><a href="#_3-2-模式" class="header-anchor">#</a> 3.2 模式</h3> <blockquote><p><strong>分组密码算法只能加密固定长度的分组，但是我们需要加密的明文长度可能会超过分组密码的分组长度，这时就需要对分组密码算法进行迭代，以便将一段很长的明文全部加密。而迭代的方法就称为分组密码的模式（mode）</strong>。</p> <p>话说到这里，很多读者可能会说：“如果明文很长的话，将明文分割成若干个分组再逐个加密不就好了吗？”事实上可没有那么简单。将明文分割成多个分组并逐个加密的方法称为ECB模式，这种模式具有很大的弱点（稍后讲解）。对密码不是很了解的程序员在编写加密软件时经常会使用ECB模式，但这样做会在不经意间产生安全漏洞，<strong>因此大家要记住千万不能使用ECB模式</strong>。</p> <p>模式有很多种类，分组密码的主要模式有以下5种：</p> <ul><li><strong>ECB模式</strong>：Electronic Code Book mode（电子密码本模式）</li> <li><strong>CBC模式</strong>：Cipher Block Chaining mode（密码分组链接模式）</li> <li><strong>CFB模式</strong>：Cipher FeedBack mode（密文反馈模式）</li> <li><strong>OFB模式</strong>：Output FeedBack mode（输出反馈模式）</li> <li><strong>CTR模式</strong>：CounTeR mode（计数器模式）</li></ul></blockquote> <h4 id="明文分组和密文分组"><a href="#明文分组和密文分组" class="header-anchor">#</a> 明文分组和密文分组</h4> <blockquote><p>在介绍模式之前，我们先来学习两个术语。</p> <p>**明文分组: **是指分组密码算法中作为加密对象的明文。明文分组的长度与分组密码算法的分组长度是相等的。</p> <p>**密文分组: **是指使用分组密码算法将明文分组加密之后所生成的密文。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538726574548.png" alt="1538726574548"></p> <blockquote><p><strong>为了避免图示变得复杂，以后我们将“用分组密码算法加密&quot;简写为“加密&quot;，并省略对密钥的描述。</strong></p></blockquote> <h3 id="_3-3-ecb-模式"><a href="#_3-3-ecb-模式" class="header-anchor">#</a> 3.3 ECB 模式</h3> <blockquote><p>ECB(Electronic Code Book, 电子密码本)模式是最简单的加密模式，<font color="red">明文消息被分成固定大小的块（分组），并且每个块被单独加密。</font>  每个块的加密和解密都是独立的，且使用相同的方法进行加密，所以可以进行并行计算，但是这种方法一旦有一个块被破解，使用相同的方法可以解密所有的明文数据，<font color="red">安全性比较差。  适用于数据较少的情形，加密前需要把明文数据填充到块大小的整倍数。</font></p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538726659781.png" alt="1538726659781"></p> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538841565454.png" alt="1538841565454"></p> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538841644005.png" alt="1538841644005"></p> <blockquote><p>使用ECB模式加密时，相同的明文分组会被转换为相同的密文分组，也就是说，我们可以将其理解为是一个巨大的“明文分组--&gt;密文分组&quot;的对应表，因此ECB模式也称为电子密码本模式<strong>当最后一个明文分组的内容小于分组长度时，需要用一特定的数据进行填充（padding），让值一个分组长度等于分组长度</strong>。</p> <p>ECB模式是所有模式中最简单的一种。ECB模式中，明文分组与密文分组是一一对应的关系，因此，如果明文中存在多个相同的明文分组，则这些明文分组最终都将被转换为相同的密文分组。这样一来，只要观察一下密文，就可以知道明文中存在怎样的重复组合，并可以以此为线索来破译密码，因此ECB模式是存在一定风险的。</p></blockquote> <h3 id="_3-3-cbc模式"><a href="#_3-3-cbc模式" class="header-anchor">#</a> 3.3 CBC模式</h3> <h4 id="xor"><a href="#xor" class="header-anchor">#</a> XOR</h4> <blockquote><p>为了让大家理解比特序列运算的概念，我们来介绍一下XOR运算。XOR的全称是exclusive or，在中文里叫作异或。尽管名字看起来很复杂，但这种运算本身一点都不难。</p> <p>1个比特（bit）的位运算规则如下：</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538724648843.png" alt="1538724648843"></p> <blockquote><p>如果将0理解为偶数， 1理解为奇数，就可以将XOR和一般的加法运算等同起来。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538724807558.png" alt="1538724807558"></p> <blockquote><p>由于XOR和加法运算很相似，因此一般用+和O组合而成的符号⊕来表示XOR。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538724868818.png" alt="1538724868818"></p> <blockquote><p>为了更加直观地理解XOR，大家可以想象一下黑白棋（奥赛罗棋）中的棋子。</p> <ul><li><p>将一个棋子保持原状（不翻转）看做0</p></li> <li><p>将一个棋子翻转到另一面看做1</p></li></ul> <p>那么XOR运算就相当于将黑白棋的一个棋子进行翻转的操作。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538726309879.png" alt="1538726309879"></p> <blockquote><p>通过上述场景，大家应该能够理解这样一个规律，<font color="red">即两个相同的数进行XOR运算的结果一定为0，因为棋子翻转两次和一次都没有翻转的结果是一样的</font>。</p> <p>上面我们介绍了1个比特之间的XOR运算，而如果是长比特序列之间的运算，则只要对其中每个相对应的比特进行XOR运算就可以了。假设我们将01001100这个比特序列称为A，将10101010这个比特序列称为B，那么A与B的XOR运算就可以像下面这样逐一对各个比特进行计算。和加法运算不同的是，XOR中不需要进位。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538725125168.png" alt="1538725125168"></p> <blockquote><p>由于两个相同的数进行XOR运算的结果一定为0，因此如果将A⊕B的结果再与B进行XOR运算，则结果会变回A。也就是说，两个公式中的B会相互抵消。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538725214357.png" alt="1538725214357"></p> <blockquote><p>可能大家已经发现了，上面的计算和加密、解密的步骤非常相似。</p> <ul><li><p>将明文A用密钥B进行加密，得到密文A⊕B</p></li> <li><p>将密文A⊕B用密钥B进行解密，得到明文A</p></li></ul> <p>实际上，只要选择一个合适的B，仅仅使用XOR就可以实现一个高强度的密码。</p> <p>对同一个比特序列进行两次XOR之后就会回到最初的状态。</p></blockquote> <h4 id="cbc模式"><a href="#cbc模式" class="header-anchor">#</a> CBC模式</h4> <blockquote><p>CBC(Cipher Block Chaining, 密码块链)<font color="red">模式中每一个分组要先和前一个分组加密后的数据进行XOR异或操作，然后再进行加密</font>。  这样每个密文块依赖该块之前的所有明文块，为了保持每条消息都具有唯一性，<font color="red">第一个数据块进行加密之前需要用初始化向量IV进行异或操作</font>。  <font color="blue">CBC模式是一种最常用的加密模式，它主要缺点是加密是连续的，不能并行处理，并且与ECB一样消息块必须填充到块大小的整倍数。</font></p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538842682058.png" alt="1538842682058"></p> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538841692606.png" alt="1538841692606"></p> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538841708069.png" alt="1538841708069"></p> <blockquote><p>如果将一个分组的加密过程分离出来，我们就可以很容易地比较出ECB模式和CBC模式的区别 。ECB模式只进行了加密，而CBC模式则在加密之前进行了一次XOR。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538727153393.png" alt="1538727153393"></p> <h4 id="初始化向量"><a href="#初始化向量" class="header-anchor">#</a> 初始化向量</h4> <blockquote><p><strong>当加密第一个明文分组时，由于不存在“前一个密文分组&quot;，因此<font color="red">需要事先准备一个长度为一个分组的比特序列来代替“前一个密文分组</font>&quot;，这个比特序列称为初始化向量（initialization vector）</strong></p> <p>通常缩写为 IV 一般来说，每次加密时都会随机产生一个不同的比特序列来作为初始化向量。</p></blockquote> <p>明文分组在加密之前一定会与“前一个密文分组&quot;进行 XOR 运算，因此即便明文分组1和2的值是相等的，密文分组1和2的值也不一定是相等的。这样一来，ECB模式的缺陷在CBC模式中就不存在了。</p> <h3 id="_3-4-cfb-模式"><a href="#_3-4-cfb-模式" class="header-anchor">#</a> 3.4 CFB 模式</h3> <blockquote><p>CFB模式的全称是Cipher FeedBack模式（密文反馈模式）。在CFB模式中，<font color="red">前一个分组的密文加密后和当前分组的明文XOR异或操作生成当前分组的密文</font>。</p> <p>所谓反馈，这里指的就是返回输人端的意思，即前一个密文分组会被送回到密码算法的输入端。</p> <p>CFB模式的解密和CBC模式的加密在流程上其实是非常相似的。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538727477811.png" alt="1538727477811"></p> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538842805890.png" alt="1538842805890"></p> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538727501371.png" alt="1538727501371"></p> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538842827438.png" alt="1538842827438"></p> <blockquote><p>在ECB模式和CBC模式中，明文分组都是通过密码算法进行加密的，然而，在CFB模式中，明文分组并没有通过密码算法来直接进行加密。</p> <p>从上图可以看出，明文分组和密文分组之间并没有经过&quot;加密&quot;这一步骤。在CFB模式中，明文分和密文分组之间只有一个XOR。</p> <p>我们将CBC模式与CFB模式对比一下，就可以看出其中的差异了（如下图）。在CBC模式中，明文分组和密文分组之间有XOR和密码算法两个步骤，而在CFB模式中，明文分组和密文分组之间则只有XOR。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538708985849.png" alt="1538708985849"></p> <h4 id="初始化向量-2"><a href="#初始化向量-2" class="header-anchor">#</a> 初始化向量</h4> <blockquote><p><font color="red">在生成第一个密文分组时，由于不存在前一个输出的数据，因此需要使用初始化向量（IV）来代替</font>，这一点和CBC模式是相同的。一般来说，我们需要在每次加密时生成一个不同的随机比特序列用作初始化向量。</p></blockquote> <h4 id="cfb模式与流密码"><a href="#cfb模式与流密码" class="header-anchor">#</a> CFB模式与流密码</h4> <blockquote><p>CFB模式是通过将“明文分组”与“密码算法的输出&quot;进行XOR运算来生成“密文分组”的。</p> <p>在CFB模式中，密码算法的输出相当于一个随机比特序列。由于密码算法的输出是通过计算得到的，并不是真正的随机数，因此CFB模式不可能具各理论上不可破译的性质。</p> <p><font color="red">CFB模式中由密算法所生成的比特序列称为密钥流（key stream）</font>。在CFB模式中，密码算法就相当于用来生成密钥流的伪随机数生成器，而初始化向量相当于伪随机数生成器的“种子“。</p> <p>在CFB模式中，<font color="red">明文数据可以被逐比特加密</font>，因此我们<font color="red">可以将CFB模式看做是一种使用分组密码来实现流密码的方式</font>。</p></blockquote> <h3 id="_3-5-ofb-模式"><a href="#_3-5-ofb-模式" class="header-anchor">#</a> 3.5 OFB 模式</h3> <blockquote><p>OFB式的全称是Output-Feedback模式（输出反馈模式）。在OFB模式中，密码算法的输出会反馈到密码算法的输入中， 即上一个分组密码算法的输出是当前分组密码算法的输入（下图）。</p> <p>OFB模式并不是通过密码算法对明文直接进行加密的，而是通过将 “明文分组&quot; 和 “密码算法的输出” 进行XOR来产生 “密文分组” 的，在这一点上OFB模式和CFB模式非常相似。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538728323577.png" alt="1538728323577"></p> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538842879039.png" alt="1538842879039"></p> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538842890856.png" alt="1538842890856"></p> <h4 id="初始化向量-3"><a href="#初始化向量-3" class="header-anchor">#</a> 初始化向量</h4> <blockquote><p>和CBC模式、CFB模式一样，OFB模式中也需要使用初始化向量（IV）。一般来说，我们需要在每次加密时生成一个不同的随机比特序列用作初始化向量。</p></blockquote> <h4 id="cfb模式和ofb模式对比"><a href="#cfb模式和ofb模式对比" class="header-anchor">#</a> CFB模式和OFB模式对比</h4> <blockquote><p>OFB模式和CFB模式的区别仅仅在于密码算法的输入。</p> <p>CFB式中，密码算法的输人是前一个密文分组，也就是将密文分组反馈到密算法中，因此就有了“密文反馈模式”这个名字。</p> <p>相对地，OFB模式中，密码算法的输入则是密码算法的前一个输出，也就是将输出反馈给密码算法，因此就有了“输出反馈模式&quot;这个名字。</p> <p>如果将一个分组抽出来对CFB模式和OFB模式进行一个对比．就可以很容易看出它们之间的差异（下图）。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538711260971.png" alt="1538711260971"></p> <blockquote><p>由于CFB模式中是对密文分组进行反馈的，因此必须从第一个明文分组开始按顺序进行加密，也就是说无法跳过明文分组1而先对明文分组2进行加密。</p> <p>相对地，在OFB模式中，XOR所需要的比特序列（密钥流）可以事先通过密码算法生成，和明文分组无关。只要提前准备好所需的密钥流，则在实际从明文生成密文的过程中，就完全不需要动用密码算法了。只要将明文与密钥流进行XOR就可以了。和AES等密码算法相比，XOR运算的速度是非常快的。这就意味着只要提前准备好密钥流就可以快速完成加密。换个角度来看，生成密钥流的操作和进行XOR运算的操作是可以并行的。</p></blockquote> <h3 id="_3-6-ctr-模式"><a href="#_3-6-ctr-模式" class="header-anchor">#</a> 3.6 CTR 模式</h3> <blockquote><p>CTR模式的全称是CounTeR模式（计数器模式）。<font color="red">CTR摸式是一种通过将逐次累加的计数器进行加密来生成密钥流的流密码</font>（下图）。</p> <p>CTR模式中，每个分组对应一个逐次累加的计数器，并通过对计数器进行加密来生成密钥流。也就是说，最终的密文分组是通过将计数器加密得到的比特序列，与明文分组进行XOR而得到的。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538711701763.png" alt="1538711701763"></p> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538842916510.png" alt="1538842916510"></p> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538711715415.png" alt="1538711715415"></p> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538842931133.png" alt="1538842931133"></p> <h4 id="计数器的生成方法"><a href="#计数器的生成方法" class="header-anchor">#</a> 计数器的生成方法</h4> <blockquote><p>每次加密时都会生成一个不同的值（nonce）来作为计数器的初始值。当分组长度为128比特（16字节）时，计数器的初始值可能是像下面这样的形式。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538711880638.png" alt="1538711880638"></p> <blockquote><p>其中前8个字节为nonce（随机数），这个值在每次加密时必须都是不同的，后8个字节为分组序号，这个部分是会逐次累加的。在加密的过程中，计数器的值会产生如下变化：</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538712002359.png" alt="1538712002359"></p> <blockquote><p>按照上述生成方法，可以保证计数器的值每次都不同。由于计数器的值每次都不同，因此每个分组中将计数器进行加密所得到的密钥流也是不同的。也是说，这种方法就是用分组密码来模拟生成随机的比特序列。</p></blockquote> <h4 id="ofb模式与ctr模式对比"><a href="#ofb模式与ctr模式对比" class="header-anchor">#</a> OFB模式与CTR模式对比</h4> <blockquote><p>CTR模式和OFB模式一样，都属于流密码。如果我们将单个分组的加密过程拿出来，那么OFB模式和CTR模式之间的差异还是很容易理解的（下图）。OFB模式是将加密的输出反愦到输入，而CTR模式则是将计数器的值用作输入。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538712242324.png" alt="1538712242324"></p> <h4 id="ctr模式的特点"><a href="#ctr模式的特点" class="header-anchor">#</a> CTR模式的特点</h4> <blockquote><p>CTR模式的加密和解密使用了完全相同的结构，因此在程序实现上比较容易。这一特点和同为流密码的OFB模式是一样的。</p> <p>此外，CTR模式中可以以任意顺序对分组进行加密和解密，因此在加密和解密时需要用到的“计数器&quot;的值可以由nonce和分组序号直接计算出来。这一性质是OFB模式所不具备的。</p> <p>能够以任意顺序处理分组，就意味着能够实现并行计算。在支持并行计算的系统中，CTR模式的速度是非常快的。</p></blockquote> <h3 id="_3-7-总结"><a href="#_3-7-总结" class="header-anchor">#</a> 3.7 总结</h3> <blockquote><p>我们已经介绍了ECB、CBC、CFB、OFB和CTR模式，下面我们对这些模式的特点做一下整理。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538712917291.png" alt="1538712917291"></p> <h2 id="_4-非对称加密"><a href="#_4-非对称加密" class="header-anchor">#</a> 4. 非对称加密</h2> <div class="language-c++ extra-class"><pre class="language-text"><code>&quot;非对称加密也叫公钥密码: 使用公钥加密, 使用私钥解密&quot;
</code></pre></div><p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538732295130.png" alt="1538732295130"></p> <blockquote><p>在对称密码中，由于加密和解密的密钥是相同的，因此必须向接收者配送密钥。用于解密的密钥必须被配送给接收者，这一问题称为密钥配送问题。如果使用非对称加密也可以称为公钥密码，则无需向接收者配送用于解密的密钥，这样就解决了密钥配送问题。可以说非对称加密是密码学历史上最伟大的发明。</p> <p>非对称加密中，密钥分为加密密钥和解密密钥两种。发送者用加密密钥对消息进行加密，接收者用解密密钥对密文进行解密。要理解公钥密码，清楚地区分加密密钥和解密密钥是非常重要的。加密密钥是发送者加密时使用的，而解密密钥则是接收者解密时使用的。</p> <p>仔细思考一下加密密钥和解密密钥的区别，我们可以发现：</p> <ul><li>发送者只需要加密密钥</li> <li>接收者只需要解密密钥</li> <li>解密密钥不可以被窃听者获取</li> <li>加密密钥被窃听者获取也没问题</li></ul> <p>也就是说，解密密钥从一开始就是由接收者自己保管的，因此只要将加密密钥发给发送者就可以解决密钥配送问题了，而根本不需要配送解密密钥。</p> <p>非对称加密中，加密密钥一般是公开的。正是由于加密密钥可以任意公开，因此该密钥被称为<strong>公钥（publickey）</strong>。公钥可以通过邮件直接发送给接收者，也可以刊登在报纸的广告栏上，做成看板放在街上，或者做成网页公开给世界上任何人，而完全不必担心被窃听者窃取。</p> <p>当然，我们也没有必要非要将公钥公开给全世界所有的人，但至少我们需要将公钥发送给需要使用公钥进行加密的通信对象（也就是给自己发送密文的发送者）。</p> <p>相对地，<strong>解密密钥是绝对不能公开的，这个密钥只能由你自己来使用，因此称为私钥（privatekey）</strong>。私钥不可以被别人知道，也不可以将它发送给别人，甚至也不能发送给自己的通信对象。</p> <p>公钥和私钥是一一对应的，一对公钥和私钥统称为<strong>密钥对（keypair）</strong>。由公钥进行加密的密文，必须使用与该公钥配对的私钥才能够解密。密钥对中的两个密钥之间具有非常密切的关系(数学上的关系)一一因此公钥和私钥是不能分别单独生成的。</p> <p>公钥密码的使用者需要生成一个包括公钥和私钥的密钥对，其中公钥会被发送给别人，而私钥则仅供自己使用。稍后我们将具体尝试生成一个密钥对。</p></blockquote> <h3 id="_4-1-非对称加密通信流程"><a href="#_4-1-非对称加密通信流程" class="header-anchor">#</a> 4.1 非对称加密通信流程</h3> <blockquote><p>下面我们来看一看使用公钥密码的通信流程。和以前一样、我们还是假设Alice要给Bob发送一条消息，Alice是发送者，Bob是接收者，而这一次窃听者Eve依然能够窃所到他们之间的通信内容。</p> <p>在公非对称加密通信中，通信过程是由接收者Bob来启动的。</p> <ol><li><p>Bob生成一个包含公钥和私钥的密钥对。</p> <blockquote><p>私钥由Bob自行妥善保管。</p></blockquote></li> <li><p>Bob将自己的公钥发送给Alicea</p> <blockquote><p>Bob的公钥被窃听者Eve截获也没关系。</p> <p>将公钥发送给Alice，表示Bob请Alice用这个公钥对消息进行加密并发送给他。</p></blockquote></li> <li><p>Alice用Bob的公钥对消息进行加密。</p> <blockquote><p>加密后的消息只有用Bob的私钥才能够解密。</p> <p>虽然Alice拥有Bob的公钥，但用Bob的公钥是无法对密文进行解密的。</p></blockquote></li> <li><p>Alice将密文发送给Bobo</p> <blockquote><p>密文被窃听者Eve截获也没关系。Eve可能拥有Bob的公钥，但是用Bob的公钥是无法进行解密的。</p></blockquote></li> <li><p>Bob用自己的私钥对密文进行解密。</p> <blockquote><p>请参考下图, 看一看在Alice和Bob之间到底传输了哪些信息。其实它们之间所传输的信息只有两个：Bob的公钥以及用Bob的公钥加密的密文。由于Bob的私钥没有出现在通信内容中，因此窃听者Eve无法对密文进行解密。</p></blockquote></li></ol></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538732846031.png" alt="1538732846031"></p> <blockquote><p>窃听者Eve可能拥有Bob的公钥，但是Bob的公钥只是加密密钥，而不是解密密钥，因此窃听者Eve就无法完成解密操作。</p></blockquote> <h3 id="_4-2-rsa"><a href="#_4-2-rsa" class="header-anchor">#</a> 4.2 RSA</h3> <blockquote><p>非对称加密的密钥分为加密密钥和解密密钥，但这到底是怎样做到的呢？本节中我们来讲解现在使用最广泛的公钥密码算法一一RSA。</p> <p>RSA是一种非对称加密算法，它的名字是由它的三位开发者，即RonRivest、AdiShamir和LeonardAdleman 的姓氏的首字母组成的（Rivest-Shamir-Leonard）。</p> <p>RSA可以被用于非对称加密和数字签名，关于数字签名我们将在后面章节进行讲解。</p> <p>1983年，RSA公司为RSA算法在美国取得了专利，但现在该专利已经过期。</p></blockquote> <h4 id="_4-2-1-rsa加密"><a href="#_4-2-1-rsa加密" class="header-anchor">#</a> 4.2.1 RSA加密</h4> <blockquote><p>下面我们终于可以讲一讲非对称加密的代表—RSA的加密过程了。在RSA中，明文、密钥和密文都是数字。RSA的加密过程可以用下列公式来表达，如下。</p></blockquote> <p>$$
密文=明文 ^ E  mod     N（RSA加密）
$$</p> <blockquote><p>也就是说，RSA的密文是对代表明文的数字的E次方求modN的结果。换句话说，就是将明文自己做E次乘法，然后将其结果除以N求余数，这个余数就是密文。</p> <p>咦，就这么简单？</p> <p>对，就这么简单。仅仅对明文进行乘方运算并求mod即可，这就是整个加密的过程。在对称密码中，出现了很多复杂的函数和操作，就像做炒鸡蛋一样将<strong>比特序列</strong>挪来挪去，还要进行XOR(按位异或)等运算才能完成，但RSA却不同，它非常简洁。</p> <p>对了，加密公式中出现的两个数一一一E和N，到底都是什么数呢？RSA的加密是求明文的E次方modN，因此只要知道E和N这两个数，任何人都可以完成加密的运算。所以说，E和N是RSA加密的密钥，也就是说，<strong>E和N的组合就是公钥</strong>。</p> <p>不过，E和N并不是随便什么数都可以的，它们是经过严密计算得出的。顺便说一句，<strong>E是加密（Encryption）的首字母，N是数字（Number)的首字母</strong>。</p> <p>有一个很容易引起误解的地方需要大家注意一一E和N这两个数并不是密钥对（公钥和私钥的密钥对）。E和N两个数才组成了一个公钥，因此我们一般会写成 “公钥是(E，N)” 或者 “公钥是{E, N}&quot; 这样的形式，将E和N用括号括起来。</p> <p>现在大家应该已经知道，==<strong>RSA的加密就是 “求E次方的modN&quot;</strong>==，接下来我们来看看RSA的解密。</p></blockquote> <h4 id="_4-2-2-rsa解密"><a href="#_4-2-2-rsa解密" class="header-anchor">#</a> 4.2.2 RSA解密</h4> <blockquote><p>RSA的解密和加密一样简单，可以用下面的公式来表达：</p></blockquote> <p>$$
明文=密文^DmodN（RSA解密）
$$</p> <blockquote><p>也就是说，对表示密文的数字的D次方求modN就可以得到明文。换句话说，将密文自己做D次乘法，再对其结果除以N求余数，就可以得到明文。</p> <p>这里所使用的数字N和加密时使用的数字N是相同的。<strong>数D和数N组合起来就是RSA的解密密钥，因此D和N的组合就是私钥</strong>。只有知道D和N两个数的人才能够完成解密的运算。</p> <p>大家应该已经注意到，<strong>在RSA中，加密和解密的形式是相同的。加密是求 &quot;E次方的mod N”，而解密则是求 &quot;D次方的modN”</strong>，这真是太美妙了。</p> <p>当然，D也并不是随便什么数都可以的，作为解密密钥的D，和数字E有着相当紧密的联系。否则，用E加密的结果可以用D来解密这样的机制是无法实现的。</p> <p>顺便说一句，<strong>D是解密〈Decryption）的首字母，N是数字（Number）的首字母</strong>。</p> <p>我们将上面讲过的内容整理一下，如下表所示。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538732904820.png" alt="1538732904820"></p> <div class="language- extra-class"><pre><code>                                                                               **RSA的加密和解密**
</code></pre></div><p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538732995510.png" alt="1538732995510"></p> <h4 id="_4-2-3-go中生成公钥和私钥"><a href="#_4-2-3-go中生成公钥和私钥" class="header-anchor">#</a> 4.2.3 Go中生成公钥和私钥</h4> <p>需要引入的包</p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;crypto/rsa&quot;</span>
	<span class="token string">&quot;crypto/rand&quot;</span>
	<span class="token string">&quot;crypto/x509&quot;</span>
	<span class="token string">&quot;encoding/pem&quot;</span>
	<span class="token string">&quot;os&quot;</span>
<span class="token punctuation">)</span>
</code></pre></div><p>生成私钥操作流程概述</p> <blockquote><ol><li>使用rsa中的GenerateKey方法生成私钥</li> <li>通过x509标准将得到的ras私钥序列化为ASN.1 的 DER编码字符串</li> <li>将私钥字符串设置到pem格式块中</li> <li>通过pem将设置好的数据进行编码, 并写入磁盘文件中</li></ol></blockquote> <p>生成公钥操作流程</p> <blockquote><ol><li>从得到的私钥对象中将公钥信息取出</li> <li>通过x509标准将得到 的rsa公钥序列化为字符串</li> <li>将公钥字符串设置到pem格式块中</li> <li>通过pem将设置好的数据进行编码, 并写入磁盘文件</li></ol></blockquote> <p>生成公钥和私钥的源代码</p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token comment">// 参数bits: 指定生成的秘钥的长度, 单位: bit</span>
<span class="token keyword">func</span> <span class="token function">RsaGenKey</span><span class="token punctuation">(</span>bits <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token builtin">error</span><span class="token punctuation">{</span>
	<span class="token comment">// 1. 生成私钥文件</span>
	<span class="token comment">// GenerateKey函数使用随机数据生成器random生成一对具有指定字位数的RSA密钥</span>
	<span class="token comment">// 参数1: Reader是一个全局、共享的密码用强随机数生成器</span>
	<span class="token comment">// 参数2: 秘钥的位数 - bit</span>
	privateKey<span class="token punctuation">,</span> err <span class="token operator">:=</span> rsa<span class="token punctuation">.</span><span class="token function">GenerateKey</span><span class="token punctuation">(</span>rand<span class="token punctuation">.</span>Reader<span class="token punctuation">,</span> bits<span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span><span class="token punctuation">{</span>
		<span class="token keyword">return</span> err
	<span class="token punctuation">}</span>
	<span class="token comment">// 2. MarshalPKCS1PrivateKey将rsa私钥序列化为ASN.1 PKCS#1 DER编码</span>
	derStream <span class="token operator">:=</span> x509<span class="token punctuation">.</span><span class="token function">MarshalPKCS1PrivateKey</span><span class="token punctuation">(</span>privateKey<span class="token punctuation">)</span>
	<span class="token comment">// 3. Block代表PEM编码的结构, 对其进行设置</span>
	block <span class="token operator">:=</span> pem<span class="token punctuation">.</span>Block<span class="token punctuation">{</span>
		Type<span class="token punctuation">:</span> <span class="token string">&quot;RSA PRIVATE KEY&quot;</span><span class="token punctuation">,</span><span class="token comment">//&quot;RSA PRIVATE KEY&quot;,</span>
		Bytes<span class="token punctuation">:</span> derStream<span class="token punctuation">,</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// 4. 创建文件</span>
	privFile<span class="token punctuation">,</span> err <span class="token operator">:=</span> os<span class="token punctuation">.</span><span class="token function">Create</span><span class="token punctuation">(</span><span class="token string">&quot;private.pem&quot;</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span><span class="token punctuation">{</span>
		<span class="token keyword">return</span> err
	<span class="token punctuation">}</span>
	<span class="token comment">// 5. 使用pem编码, 并将数据写入文件中</span>
	err <span class="token operator">=</span> pem<span class="token punctuation">.</span><span class="token function">Encode</span><span class="token punctuation">(</span>privFile<span class="token punctuation">,</span> <span class="token operator">&amp;</span>block<span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span><span class="token punctuation">{</span>
		<span class="token keyword">return</span> err
	<span class="token punctuation">}</span>
	<span class="token comment">// 6. 最后的时候关闭文件</span>
	<span class="token keyword">defer</span> privFile<span class="token punctuation">.</span><span class="token function">Close</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

	<span class="token comment">// 7. 生成公钥文件</span>
	publicKey <span class="token operator">:=</span> privateKey<span class="token punctuation">.</span>PublicKey
	derPkix<span class="token punctuation">,</span> err <span class="token operator">:=</span> x509<span class="token punctuation">.</span><span class="token function">MarshalPKIXPublicKey</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>publicKey<span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span><span class="token punctuation">{</span>
		<span class="token keyword">return</span> err
	<span class="token punctuation">}</span>
	block <span class="token operator">=</span> pem<span class="token punctuation">.</span>Block<span class="token punctuation">{</span>
		Type<span class="token punctuation">:</span> <span class="token string">&quot;RSA PUBLIC KEY&quot;</span><span class="token punctuation">,</span><span class="token comment">//&quot;PUBLIC KEY&quot;,</span>
		Bytes<span class="token punctuation">:</span> derPkix<span class="token punctuation">,</span>
	<span class="token punctuation">}</span>
	pubFile<span class="token punctuation">,</span> err <span class="token operator">:=</span> os<span class="token punctuation">.</span><span class="token function">Create</span><span class="token punctuation">(</span><span class="token string">&quot;public.pem&quot;</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span><span class="token punctuation">{</span>
		<span class="token keyword">return</span> err
	<span class="token punctuation">}</span>
	<span class="token comment">// 8. 编码公钥, 写入文件</span>
	err <span class="token operator">=</span> pem<span class="token punctuation">.</span><span class="token function">Encode</span><span class="token punctuation">(</span>pubFile<span class="token punctuation">,</span> <span class="token operator">&amp;</span>block<span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span><span class="token punctuation">{</span>
		<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
		<span class="token keyword">return</span> err
	<span class="token punctuation">}</span>
	<span class="token keyword">defer</span> pubFile<span class="token punctuation">.</span><span class="token function">Close</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

	<span class="token keyword">return</span> <span class="token boolean">nil</span>

<span class="token punctuation">}</span>
</code></pre></div><p>重要的函数介绍:</p> <ol><li><p>GenerateKey函数使用随机数据生成器random生成一对具有指定字位数的RSA密钥。</p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token string">&quot;crypto/rsa&quot;</span> 包中的函数
<span class="token keyword">func</span> <span class="token function">GenerateKey</span><span class="token punctuation">(</span>random io<span class="token punctuation">.</span>Reader<span class="token punctuation">,</span> bits <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>priv <span class="token operator">*</span>PrivateKey<span class="token punctuation">,</span> err <span class="token builtin">error</span><span class="token punctuation">)</span>
    <span class="token operator">-</span> 参数<span class="token number">1</span><span class="token punctuation">:</span> io<span class="token punctuation">.</span>Reader<span class="token punctuation">:</span> 赋值为<span class="token punctuation">:</span> rand<span class="token punctuation">.</span>Reader
        <span class="token operator">--</span> rand包实现了用于加解密的更安全的随机数生成器。
        <span class="token operator">--</span> <span class="token keyword">var</span> Reader io<span class="token punctuation">.</span>Reader <span class="token punctuation">(</span>rand包中的变量<span class="token punctuation">)</span>
    <span class="token operator">-</span> 参数<span class="token number">2</span><span class="token punctuation">:</span> bits<span class="token punctuation">:</span> 秘钥长度
    <span class="token operator">-</span> 返回值<span class="token number">1</span><span class="token punctuation">:</span> 代表一个RSA私钥。
    <span class="token operator">-</span> 返回值<span class="token number">2</span><span class="token punctuation">:</span> 错误信息

</code></pre></div></li> <li><p>通过x509 将rsa私钥序列化为ASN.1 PKCS#1 DER编码</p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token string">&quot;crypto/x509&quot;</span> 包中的函数 <span class="token punctuation">(</span>x509包解析X<span class="token punctuation">.</span><span class="token number">509</span>编码的证书和密钥<span class="token punctuation">)</span>。
<span class="token keyword">func</span> <span class="token function">MarshalPKCS1PrivateKey</span><span class="token punctuation">(</span>key <span class="token operator">*</span>rsa<span class="token punctuation">.</span>PrivateKey<span class="token punctuation">)</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span>
    <span class="token operator">-</span> 参数<span class="token number">1</span><span class="token punctuation">:</span> 通过rsa<span class="token punctuation">.</span>GenerateKey得到的私钥
    <span class="token operator">-</span> 返回值<span class="token punctuation">:</span> 将私钥通过ASN<span class="token punctuation">.</span><span class="token number">1</span>序列化之后得到的私钥编码数据
</code></pre></div></li> <li><p>设置Pem编码结构</p> <div class="language-go extra-class"><pre class="language-go"><code>Block代表PEM编码的结构。
<span class="token keyword">type</span> Block <span class="token keyword">struct</span> <span class="token punctuation">{</span>
    Type    <span class="token builtin">string</span>            <span class="token comment">// 得自前言的类型（如&quot;RSA PRIVATE KEY&quot;）</span>
    Headers <span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token builtin">string</span> <span class="token comment">// 可选的头项，Headers是可为空的多行键值对。</span>
    Bytes   <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span>            <span class="token comment">// 内容解码后的数据，一般是DER编码的ASN.1结构</span>
<span class="token punctuation">}</span>
</code></pre></div></li> <li><p>将得到的Pem格式私钥通过文件指针写入磁盘中</p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token string">&quot;encoding/pem&quot;</span> 包中的函数
<span class="token keyword">func</span> <span class="token function">Encode</span><span class="token punctuation">(</span>out io<span class="token punctuation">.</span>Writer<span class="token punctuation">,</span> b <span class="token operator">*</span>Block<span class="token punctuation">)</span> <span class="token builtin">error</span>
    <span class="token operator">-</span> 参数<span class="token number">1</span><span class="token punctuation">:</span> 可进行写操作的IO对象<span class="token punctuation">,</span> 此处需要指定一个文件指针
    <span class="token operator">-</span> 参数<span class="token number">2</span><span class="token punctuation">:</span> 初始化完成的Pem块对象<span class="token punctuation">,</span> 即Block对象
</code></pre></div></li> <li><p>通过RSA私钥得到公钥</p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token comment">// 私钥</span>
<span class="token keyword">type</span> PrivateKey <span class="token keyword">struct</span> <span class="token punctuation">{</span>
    PublicKey            <span class="token comment">// 公钥</span>
    D         <span class="token operator">*</span>big<span class="token punctuation">.</span>Int   <span class="token comment">// 私有的指数</span>
    Primes    <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token operator">*</span>big<span class="token punctuation">.</span>Int <span class="token comment">// N的素因子，至少有两个</span>
    <span class="token comment">// 包含预先计算好的值，可在某些情况下加速私钥的操作</span>
    Precomputed PrecomputedValues
<span class="token punctuation">}</span>
<span class="token comment">// 公钥</span>
<span class="token keyword">type</span> PublicKey <span class="token keyword">struct</span> <span class="token punctuation">{</span>
    N   <span class="token operator">*</span>big<span class="token punctuation">.</span>Int <span class="token comment">// 模</span>
    E   <span class="token builtin">int</span>      <span class="token comment">// 公开的指数</span>
<span class="token punctuation">}</span>
通过私钥获取公钥
publicKey <span class="token operator">:=</span> privateKey<span class="token punctuation">.</span>PublicKey <span class="token comment">// privateKey为私钥对象</span>
</code></pre></div></li> <li><p>通过x509将公钥序列化为PKIX格式DER编码。</p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token string">&quot;crypto/x509&quot;</span> 包中的函数
<span class="token keyword">func</span> <span class="token function">MarshalPKIXPublicKey</span><span class="token punctuation">(</span>pub <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>
    <span class="token operator">-</span> 参数<span class="token number">1</span><span class="token punctuation">:</span> 通过私钥对象得到的公钥
    <span class="token operator">-</span> 返回值<span class="token number">1</span>：将公钥通过ASN<span class="token punctuation">.</span><span class="token number">1</span>序列化之后得到的编码数据
    <span class="token operator">-</span> 返回值<span class="token number">2</span><span class="token punctuation">:</span> 错误信息
</code></pre></div></li> <li><p>将公钥编码之后的数据格式化为Pem结构, 参考私钥的操作</p></li> <li><p>将得到的Pem格式公钥通过文件指针写入磁盘中</p></li> <li><p>生成的私钥和公钥文件数据</p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token comment">// 私钥文件数据</span>
<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span>BEGIN RSA PRIVATE KEY<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span>
MIICXgIBAAKBgQC5bm0DCEV<span class="token operator">+</span>EFeiLUqSshziqhSB30jXy5BWbPV5SlMq4aWiEknM
i<span class="token operator">+</span>Mw1aXic4bEsM3YyT73eWsifqZNSc<span class="token operator">/</span><span class="token number">4</span>fRaV4qz5OL8IIe9AZoGDSLX<span class="token operator">/</span>Ar9AQMJf
OHbAtdIlCGQ4d80KjpDpPs2wZkTqllWCg31d7U3DVEm5kqTGtSYIu9e7JQIDAQAB
AoGARGdn72ZtvENrEHiEufjajwMO7Zng1TpS1I79PvEcHQWAhHkaoEo6VRl7SD41
yPkv9njGsaQo0WDHGFvSTGhYm<span class="token operator">/</span>EWGrBWRPc5xXbSBg7ty9Iza9B1ekAj8VfWryen
Wje3xDOCVCDUiCcYdaSfPiJPYuWMSnNMNa<span class="token operator">+</span><span class="token number">0</span>cR921zBQg0ECQQDpCMljuH7LrpbC
NDF5q<span class="token operator">+</span>LbUWMAE2KLDPX4WmDSdZdIO3mPux3MdwOUEfrcvSBGZNB7gyaEG7goZL8G
BqL22MJHAkEAy7SqbVPoPbMPHuLI52VQ2FDp6xxSWLhjmv1ePCHGo28MDCaHeVzZ
QaxyuIbnY8A6NHfu<span class="token operator">/</span>QGwz<span class="token operator">/</span>eB941IjYNBMwJBAI9XEEl<span class="token operator">+</span>mr<span class="token operator">++</span>zIz4fdZRnGE7VqId
SmgtuL7jGNtb6YpMyyFV<span class="token operator">/</span><span class="token number">6</span>ZdLp5N0PkmfEvQh0zyBycLxeNS1Q1n16Xu<span class="token operator">/</span>tECQQCZ
dF42wdDgOfWYFMu31VETw9CTtuApya3vYhMNRXx4Pf1bYeMIf<span class="token operator">/</span>OCT8CUVbwWHwc5
<span class="token number">42</span>d73TwvTorvy9TuFgSVAkEA6F69THlTn5oIP8IWHcHuqS01fIR<span class="token operator">/</span>vGfEwQ4cFZGR
ketfieyeeF8rjn4qzwT<span class="token operator">/</span>ugwRNjkhfKmoILnIC8UhEEJdjA<span class="token operator">==</span>
<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span>END RSA PRIVATE KEY<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span>
</code></pre></div><div class="language-go extra-class"><pre class="language-go"><code><span class="token comment">// 公钥文件数据</span>
<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span>BEGIN RSA PUBLIC KEY<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span>
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC5bm0DCEV<span class="token operator">+</span>EFeiLUqSshziqhSB
<span class="token number">30</span>jXy5BWbPV5SlMq4aWiEknMi<span class="token operator">+</span>Mw1aXic4bEsM3YyT73eWsifqZNSc<span class="token operator">/</span><span class="token number">4</span>fRaV4qz5
OL8IIe9AZoGDSLX<span class="token operator">/</span>Ar9AQMJfOHbAtdIlCGQ4d80KjpDpPs2wZkTqllWCg31d7U3D
VEm5kqTGtSYIu9e7JQIDAQAB
<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span>END RSA PUBLIC KEY<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span>
</code></pre></div><p>​</p></li></ol> <h4 id="_4-2-4-go中使用rsa"><a href="#_4-2-4-go中使用rsa" class="header-anchor">#</a> 4.2.4 Go中使用RSA</h4> <ol><li><p>操作步骤</p> <ul><li><p>公钥加密</p> <blockquote><ol><li>将公钥文件中的公钥读出, 得到使用pem编码的字符串</li> <li>将得到的字符串解码</li> <li>使用x509将编码之后的公钥解析出来</li> <li>使用得到的公钥通过rsa进行数据加密</li></ol></blockquote></li> <li><p>私钥解密</p> <blockquote><ol><li>将私钥文件中的私钥读出, 得到使用pem编码的字符串</li> <li>将得到的字符串解码</li> <li>使用x509将编码之后的私钥解析出来</li> <li>使用得到的私钥通过rsa进行数据解密</li></ol></blockquote></li></ul></li> <li><p>代码实现</p> <ul><li><p>RSA公钥加密</p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">RSAEncrypt</span><span class="token punctuation">(</span>src<span class="token punctuation">,</span> filename <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span> <span class="token punctuation">{</span>
    <span class="token comment">// 1. 根据文件名将文件内容从文件中读出</span>
    file<span class="token punctuation">,</span> err <span class="token operator">:=</span> os<span class="token punctuation">.</span><span class="token function">Open</span><span class="token punctuation">(</span><span class="token function">string</span><span class="token punctuation">(</span>filename<span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token boolean">nil</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// 2. 读文件</span>
    info<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> file<span class="token punctuation">.</span><span class="token function">Stat</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    allText <span class="token operator">:=</span> <span class="token function">make</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">,</span> info<span class="token punctuation">.</span><span class="token function">Size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    file<span class="token punctuation">.</span><span class="token function">Read</span><span class="token punctuation">(</span>allText<span class="token punctuation">)</span>
    <span class="token comment">// 3. 关闭文件</span>
    file<span class="token punctuation">.</span><span class="token function">Close</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

    <span class="token comment">// 4. 从数据中查找到下一个PEM格式的块</span>
    block<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> pem<span class="token punctuation">.</span><span class="token function">Decode</span><span class="token punctuation">(</span>allText<span class="token punctuation">)</span>
    <span class="token keyword">if</span> block <span class="token operator">==</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token boolean">nil</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// 5. 解析一个DER编码的公钥</span>
    pubInterface<span class="token punctuation">,</span> err <span class="token operator">:=</span> x509<span class="token punctuation">.</span><span class="token function">ParsePKIXPublicKey</span><span class="token punctuation">(</span>block<span class="token punctuation">.</span>Bytes<span class="token punctuation">)</span>
    <span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token boolean">nil</span>
    <span class="token punctuation">}</span>
    pubKey <span class="token operator">:=</span> pubInterface<span class="token punctuation">.</span><span class="token punctuation">(</span><span class="token operator">*</span>rsa<span class="token punctuation">.</span>PublicKey<span class="token punctuation">)</span>

    <span class="token comment">// 6. 公钥加密</span>
    result<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> rsa<span class="token punctuation">.</span><span class="token function">EncryptPKCS1v15</span><span class="token punctuation">(</span>rand<span class="token punctuation">.</span>Reader<span class="token punctuation">,</span> pubKey<span class="token punctuation">,</span> src<span class="token punctuation">)</span>
    <span class="token keyword">return</span> result
<span class="token punctuation">}</span>
</code></pre></div></li> <li><p>RSA私钥解密</p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">RSADecrypt</span><span class="token punctuation">(</span>src<span class="token punctuation">,</span> filename <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span> <span class="token punctuation">{</span>
  <span class="token comment">// 1. 根据文件名将文件内容从文件中读出</span>
  file<span class="token punctuation">,</span> err <span class="token operator">:=</span> os<span class="token punctuation">.</span><span class="token function">Open</span><span class="token punctuation">(</span><span class="token function">string</span><span class="token punctuation">(</span>filename<span class="token punctuation">)</span><span class="token punctuation">)</span>
  <span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span> <span class="token boolean">nil</span>
  <span class="token punctuation">}</span>
  <span class="token comment">// 2. 读文件</span>
  info<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> file<span class="token punctuation">.</span><span class="token function">Stat</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  allText <span class="token operator">:=</span> <span class="token function">make</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">,</span> info<span class="token punctuation">.</span><span class="token function">Size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  file<span class="token punctuation">.</span><span class="token function">Read</span><span class="token punctuation">(</span>allText<span class="token punctuation">)</span>
  <span class="token comment">// 3. 关闭文件</span>
  file<span class="token punctuation">.</span><span class="token function">Close</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token comment">// 4. 从数据中查找到下一个PEM格式的块</span>
  block<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> pem<span class="token punctuation">.</span><span class="token function">Decode</span><span class="token punctuation">(</span>allText<span class="token punctuation">)</span>
  <span class="token comment">// 5. 解析一个pem格式的私钥</span>
  privateKey <span class="token punctuation">,</span> err <span class="token operator">:=</span> x509<span class="token punctuation">.</span><span class="token function">ParsePKCS1PrivateKey</span><span class="token punctuation">(</span>block<span class="token punctuation">.</span>Bytes<span class="token punctuation">)</span>
  <span class="token comment">// 6. 私钥解密</span>
  result<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> rsa<span class="token punctuation">.</span><span class="token function">DecryptPKCS1v15</span><span class="token punctuation">(</span>rand<span class="token punctuation">.</span>Reader<span class="token punctuation">,</span> privateKey<span class="token punctuation">,</span> src<span class="token punctuation">)</span>

    <span class="token keyword">return</span> result
  <span class="token punctuation">}</span>
</code></pre></div></li> <li><p><strong>重要的函数介绍</strong></p> <ol><li><p>将得到的Pem格式私钥通过文件指针写入磁盘中</p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token string">&quot;encoding/pem&quot;</span> 包中的函数
<span class="token keyword">func</span> <span class="token function">Decode</span><span class="token punctuation">(</span>data <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>p <span class="token operator">*</span>Block<span class="token punctuation">,</span> rest <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span>
    <span class="token operator">-</span> 参数 data<span class="token punctuation">:</span> 需要解析的数据块
    <span class="token operator">-</span> 返回值<span class="token number">1</span><span class="token punctuation">:</span> 从参数中解析出的PEM格式的块
    <span class="token operator">-</span> 返回值<span class="token number">2</span><span class="token punctuation">:</span> 参数data剩余的未被解码的数据
</code></pre></div></li> <li><p>解析一个DER编码的公钥 , pem中的Block结构体中的数据格式为ASN.1编码</p> <div class="language-go extra-class"><pre class="language-go"><code>函数所属的包<span class="token punctuation">:</span> <span class="token string">&quot;crypto/x509&quot;</span>
<span class="token keyword">func</span> <span class="token function">ParsePKIXPublicKey</span><span class="token punctuation">(</span>derBytes <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>pub <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> err <span class="token builtin">error</span><span class="token punctuation">)</span>
    <span class="token operator">-</span> 参数 derBytes<span class="token punctuation">:</span> 从pem的Block结构体中取的ASN<span class="token punctuation">.</span><span class="token number">1</span>编码数据
    <span class="token operator">-</span> 返回值 pub<span class="token punctuation">:</span> 接口对象<span class="token punctuation">,</span> 实际是公钥数据
    <span class="token operator">-</span> 参数 err<span class="token punctuation">:</span>   错误信息
</code></pre></div></li> <li><p>解析一个DER编码的私钥 , pem中的Block结构体中的数据格式为ASN.1编码</p> <div class="language-go extra-class"><pre class="language-go"><code>函数所属的包<span class="token punctuation">:</span> <span class="token string">&quot;crypto/x509&quot;</span>
<span class="token keyword">func</span> <span class="token function">ParsePKCS1PrivateKey</span><span class="token punctuation">(</span>der <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>key <span class="token operator">*</span>rsa<span class="token punctuation">.</span>PrivateKey<span class="token punctuation">,</span> err <span class="token builtin">error</span><span class="token punctuation">)</span>
    <span class="token operator">-</span> 参数 der<span class="token punctuation">:</span> 从pem的Block结构体中取的ASN<span class="token punctuation">.</span><span class="token number">1</span>编码数据
    <span class="token operator">-</span> 返回值 key<span class="token punctuation">:</span> 解析出的私钥
    <span class="token operator">-</span> 返回值 err<span class="token punctuation">:</span> 错误信息
</code></pre></div></li> <li><p>将接口转换为公钥</p> <div class="language-go extra-class"><pre class="language-go"><code>pubKey <span class="token operator">:=</span> pubInterface<span class="token punctuation">.</span><span class="token punctuation">(</span><span class="token operator">*</span>rsa<span class="token punctuation">.</span>PublicKey<span class="token punctuation">)</span>
    <span class="token operator">-</span> pubInterface<span class="token punctuation">:</span> ParsePKIXPublicKey函数返回的 <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span> 对象
    <span class="token operator">-</span> pubInterface<span class="token punctuation">.</span><span class="token punctuation">(</span><span class="token operator">*</span>rsa<span class="token punctuation">.</span>PublicKey<span class="token punctuation">)</span><span class="token punctuation">:</span> 将pubInterface转换为公钥类型 rsa<span class="token punctuation">.</span>PublicKey
</code></pre></div></li> <li><p>使用公钥加密数据</p> <div class="language-go extra-class"><pre class="language-go"><code>函数所属的包<span class="token punctuation">:</span> <span class="token string">&quot;crypto/rsa&quot;</span>
<span class="token keyword">func</span> <span class="token function">EncryptPKCS1v15</span><span class="token punctuation">(</span>rand io<span class="token punctuation">.</span>Reader<span class="token punctuation">,</span> pub <span class="token operator">*</span>PublicKey<span class="token punctuation">,</span> msg <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>out <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">,</span> err <span class="token builtin">error</span><span class="token punctuation">)</span>
    <span class="token operator">-</span> 参数 rand<span class="token punctuation">:</span> 随机数生成器<span class="token punctuation">,</span> 赋值为 rand<span class="token punctuation">.</span>Reader
    <span class="token operator">-</span> 参数 pub<span class="token punctuation">:</span>  非对称加密加密使用的公钥
    <span class="token operator">-</span> 参数 msg<span class="token punctuation">:</span>  要使用公钥加密的原始数据
    <span class="token operator">-</span> 返回值 out<span class="token punctuation">:</span> 加密之后的数据
    <span class="token operator">-</span> 返回值 err<span class="token punctuation">:</span> 错误信息
</code></pre></div></li> <li><p>使用私钥解密数据</p> <div class="language-go extra-class"><pre class="language-go"><code>函数所属的包<span class="token punctuation">:</span> <span class="token string">&quot;crypto/rsa&quot;</span>
<span class="token keyword">func</span> <span class="token function">DecryptPKCS1v15</span><span class="token punctuation">(</span>rand io<span class="token punctuation">.</span>Reader<span class="token punctuation">,</span> priv <span class="token operator">*</span>PrivateKey<span class="token punctuation">,</span> ciphertext <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>out <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">,</span> err <span class="token builtin">error</span><span class="token punctuation">)</span>
    <span class="token operator">-</span> 参数 rand<span class="token punctuation">:</span> 随机数生成器<span class="token punctuation">,</span> 赋值为 rand<span class="token punctuation">.</span>Reader
    <span class="token operator">-</span> 参数 priv<span class="token punctuation">:</span> 非对称加密解密使用的私钥
    <span class="token operator">-</span> 参数 ciphertext<span class="token punctuation">:</span> 需要使用私钥解密的数据
    <span class="token operator">-</span> 返回值 out<span class="token punctuation">:</span> 解密之后得到的数据
    <span class="token operator">-</span> 返回值 err<span class="token punctuation">:</span> 错误信
</code></pre></div></li></ol></li></ul></li></ol> <h3 id="_4-3-ecc椭圆曲线"><a href="#_4-3-ecc椭圆曲线" class="header-anchor">#</a> 4.3 ECC椭圆曲线</h3> <blockquote><ol><li><p>概念</p> <p>椭圆曲线密码学（英语：Elliptic curve cryptography，缩写为 ECC），一种建立公开密钥加密的算法，基于椭圆曲线数学。椭圆曲线在密码学中的使用是在1985年由Neal Koblitz和Victor Miller分别独立提出的。</p> <p>ECC的主要优势是在某些情况下它比其他的方法使用更小的密钥——比如RSA加密算法——提供相当的或更高等级的安全。</p> <p>椭圆曲线密码学的许多形式有稍微的不同，所有的都依赖于被广泛承认的解决椭圆曲线离散对数问题的困难性上。与传统的基于大质数因子分解困难性的加密方法不同，ECC通过椭圆曲线方程式的性质产生密钥。</p> <p>ECC 164位的密钥产生的一个安全级相当于RSA 1024位密钥提供的保密强度，而且计算量较小，处理速度更快，存储空间和传输带宽占用较少。目前我国<code>居民二代身份证</code>正在使用 256 位的椭圆曲线密码，虚拟货币<code>比特币</code>也选择ECC作为加密算法。</p> <p>具体算法详解参考：</p> <ul><li><p>https://www.cnblogs.com/Kalafinaian/p/7392505.html</p></li> <li><p>https://blog.csdn.net/taifei/article/details/73277247</p></li></ul></li> <li><p>数学原理</p> <p>不管是RSA还是ECC或者其它，公钥加密算法都是依赖于某个正向计算很简单（多项式时间复杂度），而逆向计算很难（指数级时间复杂度）的数学问题。</p> <p>椭圆曲线依赖的数学难题是:</p> <p><code>k为正整数，P是椭圆曲线上的点（称为基点）, k*P=Q , 已知Q和P，很难计算出k</code></p></li></ol></blockquote> <h3 id="_4-4-非对称加密解惑"><a href="#_4-4-非对称加密解惑" class="header-anchor">#</a> 4.4 非对称加密解惑</h3> <ul><li><p><strong>非对称加密比对称加密机密性更高吗?</strong></p> <blockquote><p>这个问题无法回答, 以为机密性高低是根据秘钥长度而变化的</p></blockquote></li> <li><p><strong>采用1024bit 秘钥长度的非对称加密, 和采用128bit秘钥长度的对称加密中, 是秘钥更长的非对称加密更安全吗?</strong></p> <blockquote><p>不是。</p> <p>非对称加密的密钥长度不能与对称加密的密钥长度进行直接比较。下表是一张密钥长度的比较表（本表摘自《应用密码学》），根据这张表我们可以看出，1024比特的公钥密码与128比特的对称密码相比，反而是128比特的对称密码抵御暴力破解的能力更强。</p></blockquote> <table><thead><tr><th style="text-align:center;">对称加密秘钥长度</th> <th style="text-align:center;">非对称加密秘钥长度</th></tr></thead> <tbody><tr><td style="text-align:center;">128 比特</td> <td style="text-align:center;">2304 比特</td></tr> <tr><td style="text-align:center;">112 比特</td> <td style="text-align:center;">1792 比特</td></tr> <tr><td style="text-align:center;">80 比特</td> <td style="text-align:center;">768 比特</td></tr> <tr><td style="text-align:center;">64 比特</td> <td style="text-align:center;">512 比特</td></tr> <tr><td style="text-align:center;">56 比特</td> <td style="text-align:center;">384 比特</td></tr></tbody></table></li> <li><p><strong>有了非对称加密， 以后对称加密会被替代吗？</strong></p> <blockquote><p>不会。</p> <p>一般来说，在采用具备同等机密性的密钥长度的情况下，非对称加密的处理速度只有对称加密的几百分之一。因此，非对称加密并不适合用来对很长的消息内容进行加密。根据目的的不同，还可能会配合使用对称加密和非对称加密，例如，混合密码系统就是将这两种密码组合而成的。</p></blockquote></li></ul> <h2 id="_5-单向散列函数"><a href="#_5-单向散列函数" class="header-anchor">#</a> 5. 单向散列函数</h2> <div class="language-go extra-class"><pre class="language-go"><code><span class="token string">&quot;单向散列函数 --- 获取消息的指纹&quot;</span>
</code></pre></div><blockquote><p>在刑事侦查中，侦查员会用到指纹。通过将某个特定人物的指纹与犯罪现场遗留的指纹进行对比，就能够知道该人物与案件是否存在关联。</p> <p>针对计算机所处理的消息，有时候我们也需要用到“指纹&quot;。当需要比较两条消息是否一致时，我们不必直接对比消息本身的内容，只要对比它们的“指纹”就可以了。</p> <p>本章中，我们将学习单向散列函数的相关知识。使用单向散列函数就可以获取消息的“指纹”，通过对比 &quot;指纹&quot;，就能够知道两条消息是否一致。</p> <p>下面，我们会先简单介绍一下单向散列函数，并给大家展示具体的例子。然后我们将详细介绍现在使用非常广泛的SHA-I单向散列函数。</p></blockquote> <h3 id="_5-1-什么是单向散列函数"><a href="#_5-1-什么是单向散列函数" class="header-anchor">#</a> 5.1 什么是单向散列函数</h3> <blockquote><p>单向散列函数（one-wayftnction）有一个输人和一个输出，其中输人称为消息（message），输出称为散列值（hashvalue）。单向散列函数可以根据消息的内容计算出散列值，而散列值就可以被用来检查消息的完整性。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538733101252.png" alt="1538733101252"></p> <blockquote><p>这里的消息不一定是人类能够读懂的文字，也可以是图像文件或者声音文件。单向散列函数不需要知道消息实际代表的含义。无论任何消息，单向散列函数都会将它作为单纯的比特序列来处理，即根据比特序列计算出散列值。</p> <p>散列值的长度和消息的长度无关。无论消息是1比特，还是100MB，甚至是IOOGB，单向散列函数都会计算出固定长度的散列值。以SHA-I单向散列函数为例，它所计算出的散列值的长度永远是160比特（20字节）。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538733147410.png" alt="1538733147410"></p> <h3 id="_5-2-关于术语"><a href="#_5-2-关于术语" class="header-anchor">#</a> 5.2 关于术语</h3> <blockquote><p>单向散列函数的相关术语有很多变体，不同参考资料中所使用的术语也不同，下面我们就介绍其中的儿个。</p> <p>单向散列函数也称为<strong>消息摘要函数</strong>（message digest function）、<strong>哈希函数</strong>或者<strong>杂凑函数</strong>。</p> <p>输人单向散列函数的消息也称为<strong>原像</strong>（pre-image）。</p> <p>单向散列函数输出的散列值也称为<strong>消息摘要</strong>（message digest）或者<strong>指纹</strong>（fingerprint）。</p> <p><strong>完整性</strong>也称为一致性。</p> <p>顺便说一句，单向散列函数中的“散列”的英文&quot;hash一词，原意是古法语中的“斧子”，后来被引申为“剁碎的肉末&quot;，也许是用斧子一通乱剁再搅在一起的那种感觉吧。单向散列函数的作用，实际上就是将很长的消息剁碎，然后再混合成固定长度的散列值。</p></blockquote> <h3 id="_5-3-单向散列函数的性质"><a href="#_5-3-单向散列函数的性质" class="header-anchor">#</a> 5.3 单向散列函数的性质</h3> <blockquote><p>通过使用单向散列函数，即便是确认几百MB大小的文件的完整性，也只要对比很短的散列值就可以了。那么，单向散列函数必须具备怎样的性质呢？我们来整理一下。</p></blockquote> <ul><li><p><strong>根据任意长度的消息计算出固定长度的散列值</strong></p> <blockquote><p>首先，单向散列函数的输人必须能够是任意长度的消息。</p> <p>其次，无论输人多长的消息，单向散列函数必须都能够生成长度很短的散列值，如果消息越长生成的散列值也越长的话就不好用了。从使用方便的角度来看，散列值的长度最好是短且固定的。</p></blockquote></li> <li><p><strong>能够快速计算出散列值</strong></p> <blockquote><p>计算散列值所花费的时间必须要短。尽管消息越长，计算散列值的时间也会越长，但如果不能在现实的时间内完成计算就没有意义了。</p></blockquote></li> <li><p><strong>消息不同散列值也不同</strong></p> <blockquote><p>为了能够确认完整性，消息中哪怕只有1比特的改变，也必须有很高的概率产生不同的散列值。</p> <p>如果单向散列函数计算出的散列值没有发生变化，那么消息很容易就会被篡改，这个单向散列函数也就无法被用于完整性的检查。<strong>两个不同的消息产生同一个散列值的情况称为碰撞（collision）</strong>。如果要将单向散列函数用于完整性的检查，则需要确保在事实上不可能被人为地发现碰撞。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538733187999.png" alt="1538733187999"></p> <blockquote><p><strong>难以发现碰撞的性质称为抗碰撞性（collisionresistance）</strong>。密码技术中所使用的单向散列函数，都需要具备抗碰撞性。</p> <p>强抗碰撞性，是指要找到散列值相同的两条不同的消息是非常困难的这一性质。在这里，散列值可以是任意值。密码技术中的单向散列函数必须具备强抗碰撞性。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538733240065.png" alt="1538733240065"></p></li> <li><p><strong>具备单向性</strong></p> <blockquote><p>单向散列函数必须具备单向性（one-way）。单向性指的是无法通过散列值反算出消息的性质。根据消息计算散列值可以很容易，但这条单行路是无法反过来走的。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538733271733.png" alt="1538733271733"></p> <blockquote><p>正如同将玻璃砸得粉碎很容易，但却无法将碎片还原成完整的玻璃一样，根据消息计算出散列值很容易，但根据散列值却无法反算出消息。</p> <p>在这里需要注意的一点是，尽管单向散列函数所产生的散列值是和原来的消息完全不同的比特序列，但是<strong>单向散列函数并不是一种加密，因此无法通过解密将散列值还原为原来的消息</strong>。</p></blockquote></li></ul> <h3 id="_5-4-单向散列函数的实际应用"><a href="#_5-4-单向散列函数的实际应用" class="header-anchor">#</a> 5.4 单向散列函数的实际应用</h3> <blockquote><p>下面我们来看一下实际应用单向散列函数的例子。</p></blockquote> <h4 id="_5-4-1-检测软件是否被篡改"><a href="#_5-4-1-检测软件是否被篡改" class="header-anchor">#</a> 5.4.1 检测软件是否被篡改</h4> <blockquote><p>我们可以使用单向散列函数来确认自己下载的软件是否被篡改。</p> <p>很多软件，尤其是安全相关的软件都会把通过单向散列函数计算出的散列值公布在自己的官方网站上。用户在下载到软件之后，可以自行计算散列值，然后与官方网站上公布的散列值进行对比。通过散列值，用户可以确认自己所下载到的文件与软件作者所提供的文件是否一致。</p> <p>这样的方法，在可以通过多种途径得到软件的情况下非常有用。为了减轻服务器的压力，很多软件作者都会借助多个网站（镜像站点）来发布软件，在这种情况下，单向散列函数就会在检测软件是否被篡改方面发挥重要作用。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538733447564.png" alt="1538733447564"></p> <h4 id="_5-4-2-消息认证码"><a href="#_5-4-2-消息认证码" class="header-anchor">#</a> 5.4.2 消息认证码</h4> <blockquote><p>使用单向散列函数可以构造消息认证码。</p> <p>消息认证码是将“发送者和接收者之间的共享密钥”和“消息，进行混合后计算出的散列值。使用消息认证码可以检测并防止通信过程中的错误、篡改以及伪装。</p> <p>消息认证码在SSL/TLS中也得到了运用，关于SSL/TLS我们将后边章节中介绍。</p></blockquote> <h4 id="_5-4-3-数字签名"><a href="#_5-4-3-数字签名" class="header-anchor">#</a> 5.4.3 数字签名</h4> <blockquote><p>在进行数字签名时也会使用单向散列函数。</p> <p>数字签名是现实社会中的签名（sign）和盖章这样的行为在数字世界中的实现。数字签名的处理过程非常耗时，因此一般不会对整个消息内容直接施加数字签名，而是先通过单向散列函数计算出消息的散列值，然后再对这个散列值施加数字签名。</p></blockquote> <h4 id="_5-4-6-伪随机数生成器"><a href="#_5-4-6-伪随机数生成器" class="header-anchor">#</a> 5.4.6 伪随机数生成器</h4> <blockquote><p>使用单向散列函数可以构造伪随机数生成器。</p> <p>密码技术中所使用的随机数需要具备“事实上不可能根据过去的随机数列预测未来的随机数列”这样的性质。为了保证不可预测性，可以利用单向散列函数的单向性。</p></blockquote> <h4 id="_5-4-7-一次性口令"><a href="#_5-4-7-一次性口令" class="header-anchor">#</a> 5.4.7 一次性口令</h4> <blockquote><p>使用单向散列函数可以构造一次性口令（one-time password）。</p> <p>一次性口令经常被用于服务器对客户端的合法性认证。在这种方式中，通过使用单向散列函数可以保证口令只在通信链路上传送一次（one-time），因此即使窃听者窃取了口令，也无法使用。</p></blockquote> <h3 id="_5-5-常用的单向散列函数"><a href="#_5-5-常用的单向散列函数" class="header-anchor">#</a> 5.5 常用的单向散列函数</h3> <h4 id="_5-5-1-md4、md5"><a href="#_5-5-1-md4、md5" class="header-anchor">#</a> 5.5.1 MD4、MD5</h4> <blockquote><p>MD4是由Rivest于1990年设计的单向散列函数，能够产生128比特的散列值（RFC1186，修订版RFC1320）。不过，随着Dobbertin提出寻找MD4散列碰撞的方法，因此现在它已经不安全了。</p> <p>MD5是由Rwest于1991年设计的单项散列函数，能够产生128比特的散列值（RFC1321）。</p> <p>MD5的强抗碰撞性已经被攻破，也就是说，现在已经能够产生具备相同散列值的两条不同的消息，因此它也已经不安全了。</p> <p>MD4和MD5中的MD是消息摘要（Message Digest）的缩写。</p></blockquote> <p>####5.5.2 Go中使用MD5</p> <ul><li><p><strong>需要导入的包</strong></p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;crypto/md5&quot;</span>
	<span class="token string">&quot;encoding/hex&quot;</span>
<span class="token punctuation">)</span>
</code></pre></div></li> <li><p><strong>计算Md5的方式1</strong></p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">getMD5_1</span><span class="token punctuation">(</span>str <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token builtin">string</span> <span class="token punctuation">{</span>
	<span class="token comment">// 1. 计算数据的md5</span>
	result <span class="token operator">:=</span> md5<span class="token punctuation">.</span><span class="token function">Sum</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%x\n&quot;</span><span class="token punctuation">,</span> result<span class="token punctuation">)</span>
	<span class="token comment">// 2. 数据格式化为16进制格式字符串</span>
	res <span class="token operator">:=</span> fmt<span class="token punctuation">.</span><span class="token function">Sprintf</span><span class="token punctuation">(</span><span class="token string">&quot;%x&quot;</span><span class="token punctuation">,</span> result<span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span>
	<span class="token comment">// --- 这是另外一种格式化切片的方式</span>
	res <span class="token operator">=</span> hex<span class="token punctuation">.</span><span class="token function">EncodeToString</span><span class="token punctuation">(</span>result<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;res: &quot;</span><span class="token punctuation">,</span> res<span class="token punctuation">)</span>
	<span class="token keyword">return</span>  res
<span class="token punctuation">}</span>
</code></pre></div><p>重要函数说明:</p> <ol><li><p>返回数据data的MD5校验和</p> <div class="language-go extra-class"><pre class="language-go"><code>函数所属的包<span class="token punctuation">:</span> <span class="token string">&quot;crypto/md5&quot;</span>
<span class="token keyword">func</span> <span class="token function">Sum</span><span class="token punctuation">(</span>data <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token punctuation">[</span>Size<span class="token punctuation">]</span><span class="token builtin">byte</span>
    <span class="token operator">-</span> 参数 data<span class="token punctuation">:</span> 原始数据
    <span class="token operator">-</span> 返回值<span class="token punctuation">:</span> 经过md5计算之后得到的数据<span class="token punctuation">,</span> 长度为 <span class="token number">16</span>字节<span class="token punctuation">(</span><span class="token builtin">byte</span><span class="token punctuation">)</span>
</code></pre></div></li> <li><p>将字符串编码为16进制格式</p> <div class="language-go extra-class"><pre class="language-go"><code>函数所属的包<span class="token punctuation">:</span> <span class="token string">&quot;encoding/hex&quot;</span>
<span class="token keyword">func</span> <span class="token function">EncodeToString</span><span class="token punctuation">(</span>src <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token builtin">string</span>
    <span class="token operator">-</span> 参数 src<span class="token punctuation">:</span> 要转换的数据
    <span class="token operator">-</span> 返回值<span class="token punctuation">:</span> 转换之后得到的<span class="token number">16</span>进制格式字符串
</code></pre></div></li></ol></li> <li><p><strong>计算Md5的方式2</strong></p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">getMD5_2</span><span class="token punctuation">(</span>str <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token builtin">string</span> <span class="token punctuation">{</span>
	<span class="token comment">// 1. 创建一个使用MD5校验的Hash对象`</span>
	myHash <span class="token operator">:=</span> md5<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token comment">// 2. 通过io操作将数据写入hash对象中</span>
	io<span class="token punctuation">.</span><span class="token function">WriteString</span><span class="token punctuation">(</span>myHash<span class="token punctuation">,</span> <span class="token string">&quot;hello&quot;</span><span class="token punctuation">)</span>
	<span class="token comment">//io.WriteString(myHash, &quot;, world&quot;)</span>
	myHash<span class="token punctuation">.</span><span class="token function">Write</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token function">byte</span><span class="token punctuation">(</span><span class="token string">&quot;, world&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
	<span class="token comment">// 3. 计算结果</span>
	result <span class="token operator">:=</span> myHash<span class="token punctuation">.</span><span class="token function">Sum</span><span class="token punctuation">(</span><span class="token boolean">nil</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span>
	<span class="token comment">// 4. 将结果转换为16进制格式字符串</span>
	res <span class="token operator">:=</span> fmt<span class="token punctuation">.</span><span class="token function">Sprintf</span><span class="token punctuation">(</span><span class="token string">&quot;%x&quot;</span><span class="token punctuation">,</span> result<span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span>
	<span class="token comment">// --- 这是另外一种格式化切片的方式</span>
	res <span class="token operator">=</span> hex<span class="token punctuation">.</span><span class="token function">EncodeToString</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span>

	<span class="token keyword">return</span> res
<span class="token punctuation">}</span>
</code></pre></div><p>重要函数说明:</p> <ol><li><p>创建一个新的使用MD5校验的hash.Hash接口</p> <div class="language-go extra-class"><pre class="language-go"><code>函数所属的包<span class="token punctuation">:</span> <span class="token string">&quot;crypto/md5&quot;</span>
<span class="token keyword">func</span> <span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span> hash<span class="token punctuation">.</span>Hash
</code></pre></div><p>Hash是一个被所有hash函数实现的公共接口。</p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token keyword">type</span> Hash <span class="token keyword">interface</span> <span class="token punctuation">{</span>
    <span class="token comment">// 通过嵌入的匿名io.Writer接口的Write方法向hash中添加更多数据，永远不返回错误</span>
    io<span class="token punctuation">.</span>Writer
    <span class="token comment">// 返回添加b到当前的hash值后的新切片，不会改变底层的hash状态</span>
    <span class="token function">Sum</span><span class="token punctuation">(</span>b <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span>
    <span class="token comment">// 重设hash为无数据输入的状态</span>
    <span class="token function">Reset</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token comment">// 返回Sum会返回的切片的长度</span>
    <span class="token function">Size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">int</span>
    <span class="token comment">// 返回hash底层的块大小；Write方法可以接受任何大小的数据，</span>
    <span class="token comment">// 但提供的数据是块大小的倍数时效率更高</span>
    <span class="token function">BlockSize</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">int</span>
<span class="token punctuation">}</span>

<span class="token string">&quot;io&quot;</span> 包中 Writer 接口用于包装基本的写入方法。
<span class="token keyword">type</span> Writer <span class="token keyword">interface</span> <span class="token punctuation">{</span>
    <span class="token function">Write</span><span class="token punctuation">(</span>p <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>n <span class="token builtin">int</span><span class="token punctuation">,</span> err <span class="token builtin">error</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div></li> <li><p>通过io操作将数据写入hash对象中</p> <div class="language-go extra-class"><pre class="language-go"><code># 第一种方式
函数所属的包<span class="token punctuation">:</span> <span class="token string">&quot;io&quot;</span>
<span class="token keyword">func</span> <span class="token function">WriteString</span><span class="token punctuation">(</span>w Writer<span class="token punctuation">,</span> s <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>n <span class="token builtin">int</span><span class="token punctuation">,</span> err <span class="token builtin">error</span><span class="token punctuation">)</span>
    <span class="token operator">-</span> 参数 w<span class="token punctuation">:</span> 实现了<span class="token operator">/</span>包含Writer接口的对象
    <span class="token operator">-</span> 参数 s<span class="token punctuation">:</span> 要添加到IO对象中的数据
    <span class="token operator">-</span> 返回值 n<span class="token punctuation">:</span> 数据长度
    <span class="token operator">-</span> 返回值 err<span class="token punctuation">:</span> 错误信息
# 第二种方式
使用md5包中的<span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>方法得到的hash<span class="token punctuation">.</span>Hash接口<span class="token punctuation">(</span>假设名为<span class="token punctuation">:</span> myHash<span class="token punctuation">)</span>添加数据
myHash<span class="token punctuation">.</span><span class="token function">Write</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token function">byte</span><span class="token punctuation">(</span><span class="token string">&quot;测试数据&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre></div></li> <li><p>使用hash.Hash接口中的Sum方法计算结果</p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token function">Sum</span><span class="token punctuation">(</span>b <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span>
    <span class="token operator">-</span> 参数 b<span class="token punctuation">:</span> 将b中的数据进行哈希计算<span class="token punctuation">,</span> 结果添加到原始数据的前面<span class="token punctuation">,</span> 
      		 一般情况下该参数指定为空<span class="token punctuation">,</span> 即<span class="token punctuation">:</span> <span class="token boolean">nil</span>
    <span class="token operator">-</span> 返回值<span class="token punctuation">:</span> 进行哈希运算之后得到的结果 
</code></pre></div></li></ol></li></ul> <h4 id="_5-5-3-sha-1、sha-224、sha-256、sha-384、sha-512"><a href="#_5-5-3-sha-1、sha-224、sha-256、sha-384、sha-512" class="header-anchor">#</a> 5.5.3 SHA-1、SHA-224、SHA-256、SHA-384、SHA-512</h4> <blockquote><p>SHA-1是由NIST（NationalInstituteOfStandardsandTechnology，美国国家标准技术研究所）设计的一种能够产生160比特的散列值的单向散列函数。1993年被作为美国联邦信息处理标准规格（FIPS PUB 180）发布的是SHA,1995年发布的修订版FIPS PUB 180-1称为SHA-1。</p> <p>SHA-1的消息长度存在上限，但这个值接近于2^64^比特，是个非常巨大的数值，因此在实际应用中没有问题。</p> <p>SHA-256、SHA-384和SHA-512都是由NIST设计的单向散列函数，它们的散列值长度分别为256比特、384比特和512比特。这些单向散列函数合起来统称SHA-2，它们的消息长度也存在上限（SHA-256的上限接近于 2^64^ 比特，SHA-384 和 SHA-512的上限接近于 2^128^ 比特）。这些单向散列函数是于2002年和 SHA-1 一起作为 FIPS PUB 180-2发布的 SHA-1 的强抗碰撞性已于2005年被攻破, 也就是说，现在已经能够产生具备相同散列值的两条不同的消息。不过，SHA-2还尚未被攻破。</p></blockquote> <table><thead><tr><th></th> <th style="text-align:center;">比特数</th> <th style="text-align:center;">字节数</th></tr></thead> <tbody><tr><td>MD4</td> <td style="text-align:center;">128bit</td> <td style="text-align:center;">16byte</td></tr> <tr><td>MD5</td> <td style="text-align:center;">128bit</td> <td style="text-align:center;">16byte</td></tr> <tr><td>SHA-1</td> <td style="text-align:center;">160bit</td> <td style="text-align:center;">20byte</td></tr> <tr><td>SHA-224</td> <td style="text-align:center;">224bit</td> <td style="text-align:center;">28byte</td></tr> <tr><td>SHA-256</td> <td style="text-align:center;">256bit</td> <td style="text-align:center;">32byte</td></tr> <tr><td>SHA-384</td> <td style="text-align:center;">384bit</td> <td style="text-align:center;">48byte</td></tr> <tr><td>SHA-512</td> <td style="text-align:center;">512bit</td> <td style="text-align:center;">64byte</td></tr></tbody></table> <p>####5.5.4 Go中对SHA-1、SHA-2的使用</p> <ul><li><p><strong>需要导入的包</strong></p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;crypto/sha1&quot;</span>
	<span class="token string">&quot;encoding/hex&quot;</span>
    <span class="token string">&quot;crypto/sha256&quot;</span>
    <span class="token string">&quot;crypto/sha512&quot;</span>
<span class="token punctuation">)</span>
</code></pre></div></li> <li><p><strong>使用sha1计算文件指纹</strong></p> <blockquote><p>上一小节介绍了如何使用go提供的API计算数据的md5指纹, sha1的计算方式和md5的套路是一样的, 需要将md5包, 替换为sh1, 下面给大家介绍一下如何使用sha1计算文件的指纹(md5亦如此)</p></blockquote> <div class="language-go extra-class"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">getSha1</span><span class="token punctuation">(</span>src <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token builtin">string</span> <span class="token punctuation">{</span>
	<span class="token comment">// 1. 打开文件</span>
	fp<span class="token punctuation">,</span> err <span class="token operator">:=</span> os<span class="token punctuation">.</span><span class="token function">Open</span><span class="token punctuation">(</span>src<span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> <span class="token string">&quot;文件打开失败&quot;</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// 2. 创建基于sha1算法的Hash对象</span>
	myHash <span class="token operator">:=</span> sha1<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token comment">// 3. 将文件数据拷贝给哈希对象</span>
	num<span class="token punctuation">,</span> err <span class="token operator">:=</span> io<span class="token punctuation">.</span><span class="token function">Copy</span><span class="token punctuation">(</span>myHash<span class="token punctuation">,</span> fp<span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> <span class="token string">&quot;拷贝文件失败&quot;</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;文件大小: &quot;</span><span class="token punctuation">,</span> num<span class="token punctuation">)</span>
	<span class="token comment">// 4. 计算文件的哈希值</span>
	tmp1 <span class="token operator">:=</span> myHash<span class="token punctuation">.</span><span class="token function">Sum</span><span class="token punctuation">(</span><span class="token boolean">nil</span><span class="token punctuation">)</span>
	<span class="token comment">// 5. 数据格式转换</span>
    result <span class="token operator">:=</span> hex<span class="token punctuation">.</span><span class="token function">EncodeToString</span><span class="token punctuation">(</span>tmp1<span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;sha1: &quot;</span><span class="token punctuation">,</span> result<span class="token punctuation">)</span>

	<span class="token keyword">return</span> result
<span class="token punctuation">}</span>
</code></pre></div></li></ul> <h2 id="_6-消息认证码"><a href="#_6-消息认证码" class="header-anchor">#</a> 6. 消息认证码</h2> <div class="language-go extra-class"><pre class="language-go"><code><span class="token string">&quot;消息认证码 --- 消息被正确传送了吗?&quot;</span>
</code></pre></div><h3 id="_6-1-什么是消息认证码"><a href="#_6-1-什么是消息认证码" class="header-anchor">#</a> 6.1 什么是消息认证码</h3> <ul><li><p><strong>Alice 和 Bob 的故事</strong></p> <blockquote><p>像以前一样，我们还是从一个Alice和Bob的故事开始讲起。不过，这一次Alice和Bob分别是两家银行，Alice银行通过网络向Bob银行发送了一条汇款请求，Bob银行收到的请求内容是：</p></blockquote> <p>$$
从账户A-5374         向账户B-6671汇款1000万元
$$</p> <blockquote><p>当然，Bob银行所收到的汇款请求内容必须与Alice银行所发送的内容是完全一致的。如果主动攻击者Mallory在中途将Alice银行发送的汇款请求进行了篡改，那么Bob银行就必须要能够识别出这种篡改，否则如果Mallory将收款账户改成了自己的账户，那么1000万元就会被盗走。</p> <p>话说回来，这条汇款请求到底是不是Alice银行发送的呢？有可能Alice银行根本就没有发送过汇款请求，而是由主动攻击者Mallory伪装成Alice银行发送的。如果汇款请求不是来自Alice银行，那么就绝对不能执行汇款。</p> <p>现在我们需要关注的问题是汇款请求（消息）的 “完整性&quot; 和 “认证&quot; 这两个性质。</p> <p><strong>消息的完整性（integrity), 指的是“消息没有被篡改&quot;这一性质，完整性也叫一致性</strong>。如果能够确认汇款请求的内容与Alice银行所发出的内容完全一致，就相当于是确认了消息的完整性，也就意味着消息没有被篡改。</p> <p><strong>消息的认证（authentication）指的是“消息来自正确的发送者&quot;这一性质</strong>。如果能够确认汇款请求确实来自Alice银行，就相当于对消息进行了认证，也就意味着消息不是其他人伪装成发送者所发出的。</p> <p>通过使用本章中要介绍的消息认证码，我们就可以同时识别出篡改和伪装，也就是既可以确认消息的完整性，也可以进行认证。</p></blockquote></li> <li><p><strong>什么是消息认证码</strong></p> <blockquote><p><strong>消息认证码（message authentication code）是一种确认完整性并进行认证的技术，取三个单词的首字母，简称为MAC。</strong></p> <p>消息认证码的输入包括任意长度的消息和一个发送者与接收者之间<strong>共享的密钥</strong>，它可以输出固定长度的数据，这个数据称为<strong>MAC值</strong>。</p> <p>根据任意长度的消息输出固定长度的数据，这一点和单向散列函数很类似。但是单向散列函数中计算散列值时不需要密钥，而消息认证码中则需要使用发送者与接收者之间共享的密钥。</p> <p>要计算MAC必须持有共享密钥，没有共享密钥的人就无法计算MAC值，消息认证码正是利用这一性质来完成认证的。此外，和单向散列函数的散列值一样，哪怕消息中发生1比特的变化，MAC值也会产生变化，消息认证码正是利用这一性质来确认完整性的。</p> <p>消息认证码有很多种实现方法，大家可以暂且这样理解：消息认证码是一种与密钥相关联的单向散列函数。</p></blockquote> <div class="language- extra-class"><pre><code>  				                           **单向散列函数与消息认证码的比较**
</code></pre></div><p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538733638766.png" alt="1538733638766"></p></li></ul> <h3 id="_6-2-消息认证码的使用步骤"><a href="#_6-2-消息认证码的使用步骤" class="header-anchor">#</a> 6.2 消息认证码的使用步骤</h3> <blockquote><p>我们还是以Alice银行和Bob银行的故事为例，来讲解一下消息认证码的使用步骤:</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538733684992.png" alt="1538733684992"></p> <blockquote><ol><li>发送者Alice与接收者Bob事先共享密钥。</li> <li>发送者Alice根据汇款请求消息计算MAC值（使用共享密钥）。</li> <li>发送者Alice将汇款请求消息和MAC值两者发送给接收者Bob。</li> <li>接收者Bob根据接收到的汇款请求消息计算MAC值（使用共享密钥）。</li> <li>接收者Bob将自己计算的MAC值与从Alice处收到的MAC值进行对比。</li> <li>如果两个MAC值一致，则接收者Bob就可以断定汇款请求的确来自Alice（认证成功）；如果不一致，则可以断定消息不是来自Alice（认证失败）。</li></ol></blockquote> <h3 id="_6-3-hmac"><a href="#_6-3-hmac" class="header-anchor">#</a> 6.3 HMAC</h3> <h4 id="_6-3-1-hmac介绍"><a href="#_6-3-1-hmac介绍" class="header-anchor">#</a> 6.3.1 HMAC介绍</h4> <blockquote><p>HMAC是一种使用单向散列函数来构造消息认证码的方法（RFC2104），其中HMAC的H就是Hash的意思。</p> <p>HMAC中所使用的单向散列函数并不仅限于一种，任何高强度的单向散列函数都可以被用于HMAC,如果将来设计出新的单向散列函数，也同样可以使用。</p> <p>使用SHA-I、MD5、RIPEMD-160所构造的HMAC，分别称为HMAC-SHA-1、HMAC-MD5和HMAC-RlPEMD。</p></blockquote> <div class="language- extra-class"><pre><code>			**使用HMAC通过秘钥将消息生成消息认证码的内部实现**：
</code></pre></div><p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538733763591.png" alt="1538733763591"></p> <blockquote><p><strong>通过上述流程我们可以看出，最后得到的MAC值，一定是一个和输入的消息以及密钥都相关的长度固定的比特序列。</strong></p></blockquote> <h4 id="_6-3-2-go中对hmac的使用"><a href="#_6-3-2-go中对hmac的使用" class="header-anchor">#</a> 6.3.2 Go中对HMAC的使用</h4> <p>需要使用的包</p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token keyword">import</span> <span class="token string">&quot;crypto/hmac&quot;</span>
</code></pre></div><p>使用的函数</p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">New</span><span class="token punctuation">(</span>h <span class="token keyword">func</span><span class="token punctuation">(</span><span class="token punctuation">)</span> hash<span class="token punctuation">.</span>Hash<span class="token punctuation">,</span> key <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> hash<span class="token punctuation">.</span>Hash
<span class="token keyword">func</span> <span class="token function">Equal</span><span class="token punctuation">(</span>mac1<span class="token punctuation">,</span> mac2 <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token builtin">bool</span>
</code></pre></div><blockquote><ol><li>hamc.New 函数
<ul><li>参数1: 创建一个新的使用哈希校验算法的hash.Hash接口, 如:
<ul><li>md5.New()</li> <li>sha1.New()</li> <li>sha256.New()</li></ul></li> <li>参数2: 使用的秘钥</li> <li>返回值: 通过该哈希接口添加数据和计算消息认证码
<ul><li>添加数据: <strong>Write(p []byte) (n int, err error)</strong></li> <li>计算结果: <strong>Sum(b []byte) []byte</strong></li></ul></li></ul></li> <li>hmac.Equal 函数
<ul><li>比较两个MAC是否相同</li></ul></li></ol></blockquote> <p>生成消息认证码:</p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token comment">// 生成消息认证码</span>
<span class="token keyword">func</span> <span class="token function">GenerateHMAC</span><span class="token punctuation">(</span>src<span class="token punctuation">,</span> key <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span> <span class="token punctuation">{</span>
	<span class="token comment">// 1. 创建一个底层采用sha256算法的 hash.Hash 接口</span>
	myHmac <span class="token operator">:=</span> hmac<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span>sha256<span class="token punctuation">.</span>New<span class="token punctuation">,</span> key<span class="token punctuation">)</span>
	<span class="token comment">// 2. 添加测试数据</span>
	myHmac<span class="token punctuation">.</span><span class="token function">Write</span><span class="token punctuation">(</span>src<span class="token punctuation">)</span>
	<span class="token comment">// 3. 计算结果</span>
	result <span class="token operator">:=</span> myHmac<span class="token punctuation">.</span><span class="token function">Sum</span><span class="token punctuation">(</span><span class="token boolean">nil</span><span class="token punctuation">)</span>

	<span class="token keyword">return</span> result
<span class="token punctuation">}</span>
</code></pre></div><p>重要函数说明</p> <ol><li><p>创建一个底层采用哈希算法的 hash.Hash 接口</p> <div class="language-go extra-class"><pre class="language-go"><code>函数对应的包<span class="token punctuation">:</span> <span class="token string">&quot;crypto/hmac&quot;</span>
<span class="token keyword">func</span> <span class="token function">New</span><span class="token punctuation">(</span>h <span class="token keyword">func</span><span class="token punctuation">(</span><span class="token punctuation">)</span> hash<span class="token punctuation">.</span>Hash<span class="token punctuation">,</span> key <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> hash<span class="token punctuation">.</span>Hash
    <span class="token operator">-</span> 参数 h<span class="token punctuation">:</span> 函数指针<span class="token punctuation">,</span> 返回值为hash<span class="token punctuation">.</span>Hash<span class="token punctuation">,</span> 可以使用哈希算法对应的New方法<span class="token punctuation">,</span> 如<span class="token punctuation">:</span>
        <span class="token operator">--</span> md5<span class="token punctuation">.</span>New
        <span class="token operator">--</span> sha1<span class="token punctuation">.</span>New
        <span class="token operator">--</span> sha256<span class="token punctuation">.</span>New
        <span class="token operator">--</span> sha256<span class="token punctuation">.</span>New224
        <span class="token operator">--</span> sha512<span class="token punctuation">.</span>New
        <span class="token operator">--</span> sha512<span class="token punctuation">.</span>New384
    <span class="token operator">-</span> 参数 key<span class="token punctuation">:</span> 和数据进行混合运算使用的秘钥
    <span class="token operator">-</span> 返回值<span class="token punctuation">:</span> hash<span class="token punctuation">.</span>Hash 接口
</code></pre></div></li></ol> <p>验证消息认证码</p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">VerifyHMAC</span><span class="token punctuation">(</span>res<span class="token punctuation">,</span> src<span class="token punctuation">,</span> key <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token builtin">bool</span> <span class="token punctuation">{</span>

	<span class="token comment">// 1. 创建一个底层采用sha256算法的 hash.Hash 接口</span>
	myHmac <span class="token operator">:=</span> hmac<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span>sha256<span class="token punctuation">.</span>New<span class="token punctuation">,</span> key<span class="token punctuation">)</span>
	<span class="token comment">// 2. 添加测试数据</span>
	myHmac<span class="token punctuation">.</span><span class="token function">Write</span><span class="token punctuation">(</span>src<span class="token punctuation">)</span>
	<span class="token comment">// 3. 计算结果</span>
	result <span class="token operator">:=</span> myHmac<span class="token punctuation">.</span><span class="token function">Sum</span><span class="token punctuation">(</span><span class="token boolean">nil</span><span class="token punctuation">)</span>
	<span class="token comment">// 4. 比较结果</span>
	<span class="token keyword">return</span> hmac<span class="token punctuation">.</span><span class="token function">Equal</span><span class="token punctuation">(</span>res<span class="token punctuation">,</span> result<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div><p>重要函数说明:</p> <ol><li><p>比较两个MAC是否相同</p> <div class="language-go extra-class"><pre class="language-go"><code>函数对应的包<span class="token punctuation">:</span> <span class="token string">&quot;crypto/hmac&quot;</span>
<span class="token keyword">func</span> <span class="token function">Equal</span><span class="token punctuation">(</span>mac1<span class="token punctuation">,</span> mac2 <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token builtin">bool</span>
    <span class="token operator">-</span> 参数 mac1<span class="token punctuation">,</span> mac2<span class="token punctuation">:</span> 通过哈希算法计算得到的消息认证码
    <span class="token operator">-</span> 返回值<span class="token punctuation">:</span> 如果mac1<span class="token operator">==</span>mac2<span class="token punctuation">,</span> 返回 <span class="token boolean">true</span><span class="token punctuation">;</span> 否则<span class="token punctuation">,</span> 返回 <span class="token boolean">false</span>
</code></pre></div></li></ol> <p>测试代码</p> <div class="language-go extra-class"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">HMacTest</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	key <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token function">byte</span><span class="token punctuation">(</span><span class="token string">&quot;我是消息认证码秘钥&quot;</span><span class="token punctuation">)</span>
	src <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token function">byte</span><span class="token punctuation">(</span><span class="token string">&quot;我是消息认证码测试数据&quot;</span><span class="token punctuation">)</span>
	result <span class="token operator">:=</span> <span class="token function">GenerateHMAC</span><span class="token punctuation">(</span>src<span class="token punctuation">,</span> key<span class="token punctuation">)</span>
	final <span class="token operator">:=</span> <span class="token function">VerifyHMAC</span><span class="token punctuation">(</span>result<span class="token punctuation">,</span> src<span class="token punctuation">,</span> key<span class="token punctuation">)</span>
	<span class="token keyword">if</span> final <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;消息认证码认证成功!!!&quot;</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;消息认证码认证失败 ......&quot;</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="_6-3-消息认证码的密钥配送问题"><a href="#_6-3-消息认证码的密钥配送问题" class="header-anchor">#</a> 6.3 消息认证码的密钥配送问题</h3> <blockquote><p>在消息认证码中，需要发送者和接收者之间共享密钥，而这个密钥不能被主动攻击者Mallory获取。如果这个密钥落入Mallory手中，则Mallory也可以计算出MAC值，从而就能够自由地进行篡改和伪装攻击，这样一来消息认证码就无法发挥作用了。</p> <p>发送者和接收者需要共享密钥，这一点和我们介绍的对称加密很相似。实际上，对称加密的密钥配送问题在消息认证码中也同样会发生。关于秘钥的配送后边章节会介绍如何使用非对称加密的方式进行解决。</p></blockquote> <h3 id="_6-4-消息认证码无法解决的问题"><a href="#_6-4-消息认证码无法解决的问题" class="header-anchor">#</a> 6.4 消息认证码无法解决的问题</h3> <blockquote><p>假设发送者Alice要向接收者Bob发送消息，如果使用了消息认证码，接收者Bob就能够断定自己收到的消息与发送者Alice所发出的消息是一致的，这是因为消息中的MAC值只有用Alice和Bob之间共享的密钥才能够计算出来，即便主动攻击者Mallory篡改消息，或者伪装成Alice发送消息，Bob也能够识别出消息的篡改和伪装。</p> <p>但是，消息认证码也不能解决所有的问题，例如“对第三方证明&quot;和“防止否认&quot;，这两个问题就无法通过消息认证码来解决。下面我们来逐一解释一下。</p></blockquote> <h4 id="_6-4-1-对第三方证明"><a href="#_6-4-1-对第三方证明" class="header-anchor">#</a> 6.4.1 对第三方证明</h4> <blockquote><p>假设Bob在接收了来自Alice的消息之后，想要向第三方验证者Victor证明这条消息的确是Alice发送的，但是用消息认证码无法进行这样的证明，这是为什么呢？</p> <p>首先，Victor要校验MAC值，就需要知道Alice和Bob之间共享的密钥。</p> <p>假设Bob相信Victor, 同意将密钥告诉Victor,即便如此，Victor也无法判断这条消息是由Alice发送的，因为Victor可以认为：“即使MAC值是正确的，发送这条消息的人也不一定是Alice，还有可能是Bob。&quot;</p> <p>能够计算出正确MAC值的人只有Alice和Bob，在他们两个人之间进行通信时，可以断定是对方计算了MAC值，这是因为共享这个密钥的双方之中，有一方就是自己。然而，对于第三方Victor、Alice或Bob却无法证明是对方计算了MAC值，而不是自己。</p> <p>使用第7章中将要介绍的数字签名就可以实现对第三方的证明。</p></blockquote> <p>####6.4.2 防止否认</p> <blockquote><p>假设Bob收到了包含MAC值的消息，这个MAC值是用Alice和Bob共享的密钥计算出来的，因此Bob能够判断这条消息的确来自Alice。</p> <p>但是，上面我们讲过，Bob无法向验证者Victor证明这一点，也就是说，发送者Alice可以向Victor声称：“我没有向Bob发送过这条消息。”这样的行为就称为否认（repudiation）。</p> <p>Alice可以说“这条消息是Bob自己编的吧&quot;，“说不定Bob的密钥被主动攻击者Mallory给盗取了，我的密钥可是妥善保管着呢&quot; 等。说白了，就是Alice和Bob吵起来了。</p> <p>即便Bob拿MAC值来举证，Victor也无法判断Alice和Bob谁的主张才是正确的，也就是说，<strong>用消息认证码无法防止否认（nonrepudiatlon）</strong>。</p></blockquote> <h3 id="_6-5-总结"><a href="#_6-5-总结" class="header-anchor">#</a> 6.5 总结</h3> <blockquote><p>消息认证码是对消息进行认证并确认其完整性的技术。通过使用发送者和接收者之间共享的密钥，就可以识别出是否存在伪装和篡改行为。</p> <p>消息认证码可以使用单向散列函数HMAC， 对称加密也可以实现， 这里不再进行介绍。</p> <p>消息认证码中，由于发送者和接收者共享相同的密钥，因此会产生无法对第三方证明以及无法防止否认等问题。在下一章中，我们将介绍能够解决这些问题的数字签名。</p></blockquote> <h2 id="_7-数字签名"><a href="#_7-数字签名" class="header-anchor">#</a> 7. 数字签名</h2> <div class="language-go extra-class"><pre class="language-go"><code><span class="token string">&quot;数字签名 --- 消息到底是谁写的&quot;</span>
</code></pre></div><blockquote><p>本章中我们将学习数字签名的相关知识。数字签名是一种将相当于现实世界中的盖章、签字的功能在计算机世界中进行实现的技术。使用数字签名可以识别篡改和伪装，还可以防止否认。</p></blockquote> <h3 id="_7-1-从消息认证到数字签名"><a href="#_7-1-从消息认证到数字签名" class="header-anchor">#</a> 7.1 从消息认证到数字签名</h3> <ul><li><p><strong>消息认证码的局限性</strong></p> <blockquote><p>通过使用第6章中介绍的消息认证码，我们可以识别消息是否被篡改或者发送者身份是否被伪装，也就是可以校验消息的完整性，还可以对消息进行认证。然而，比如在出具借条的场景中却无法使用消息认证码，因为消息认证码无法防止否认。</p> <p>消息认证码之所以无法防止否认，是因为消息认证码需要在发送者Alice和接收者Bob两者之间共享同一个密钥。正是因为密钥是共享的，所以能够使用消息认证码计算出正确MAC值的并不只有发送者Alice，接收者Bob也可以计算出正确的MAC值。由于Alice和Bob双方都能够计算出正确的MAC值，因此对于第三方来说，我们无法证明这条消息的确是由Alice生成的。</p></blockquote></li> <li><p><strong>通过数字签名解决问题</strong></p> <blockquote><p>下面请大家开动一下脑筋。假设发送者Alice和接收者Bob不需要共享一个密钥，也就是说，Alice和Bob各自使用不同的密钥。</p> <p>我们假设Alice使用的密钥是一个只有Alice自己才知道的私钥。当Alice发送消息时，她用私钥生成一个“签名&quot;。相对地，接收者Bob则使用一个和Alice不同的密钥对签名进行验证。使用Bob的密钥无法根据消息生成签名，但是用Bob的密钥却可以对Alice所计算的签名进行验证，也就是说可以知道这个签名是否是通过Alice的密钥计算出来的。如果真有这么一种方法的话，那么不管是识别篡改、伪装还是防止否认就都可以实现了吧 ？</p> <p>实际上，这种看似很神奇的技术早就已经问世了，这就是数字签名（digital signat.ure）。</p></blockquote></li></ul> <h3 id="_7-2-签名的生成和验证"><a href="#_7-2-签名的生成和验证" class="header-anchor">#</a> 7.2 签名的生成和验证</h3> <blockquote><p>让我们来稍微整理一下。</p> <p>在数字签名技术中，出现了下面两种行为：</p> <ul><li><strong>生成消息签名的行为</strong></li> <li><strong>验证消息签名的行为</strong></li></ul> <p><strong>生成消息签名</strong>这一行为是由消息的发送者Alice来完成的，也称为“对消息签名”。生成签名就是根据消息内容计算数字签名的值，这个行为意味着 “我认可该消息的内容&quot;。</p> <p><strong>验证数字签名</strong>这一行为一般是由消息的接收者Bob来完成的，但也可以由需要验证消息的第三方来完成，这里的第三方我们暂且将其命名为验证者Victor。验证签名就是检查该消息的签名是否真的属于Alice，验证的结果可以是成功或者失败，成功就意味着这个签名是属于Alice的，失败则意味着这个签名不是属于Alice的。</p> <p>在数字签名中，生成签名和验证签名这两个行为需要使用各自专用的密钥来完成。</p> <p>Alice使用“签名密钥&quot;来生成消息的签名，而Bob和Victor则使用“验证密钥&quot;来验证消息的签名。数字签名对签名密钥和验证密钥进行了区分，使用验证密钥是无法生成签名的。这一点非常重要。此外，<strong>签名密钥只能由签名的人持有</strong>，而<strong>验证密钥则是任何需要验证签名的人都可以持有</strong>。</p> <p>刚才讲的这部分内容，是不是觉得似曾相识呢？</p> <p>没错，这就是我们讲过的非对称加密。公钥密码和上面讲的数字签名的结构非常相似。在非对称加密中，密钥分为加密密钥和解密密钥，用加密密钥无法进行解密。此外，解密密钥只能由需要解密的人持有，而加密密钥则是任何需要加密的人都可以持有。你看，数字签名和非对称加密是不是很像呢？</p> <p>实际上，数字签名和非对称加密有着非常紧密的联系，简而言之，数字签名就是通过将非对称加密 “反过来用” 而实现的。下面我们来将密钥的使用方式总结成一张表：</p></blockquote> <table><thead><tr><th></th> <th style="text-align:center;">私钥</th> <th style="text-align:center;">公钥</th></tr></thead> <tbody><tr><td>非对称加密</td> <td style="text-align:center;">接收者解密时使用</td> <td style="text-align:center;">发送者加密时使用</td></tr> <tr><td>数字签名</td> <td style="text-align:center;">签名者生成签名时使用</td> <td style="text-align:center;">验证者验证签名时使用</td></tr> <tr><td>谁持有秘钥？</td> <td style="text-align:center;">个人持有</td> <td style="text-align:center;">只要需要，任何人都可以持有</td></tr></tbody></table> <h3 id="_7-3-非对称加密和数字签名"><a href="#_7-3-非对称加密和数字签名" class="header-anchor">#</a> 7.3 非对称加密和数字签名</h3> <blockquote><p>下面我们再来详细讲一讲非对称加密与数字签名之间的关系。</p> <p>要实现数字签名，我们可以使用第4章中介绍的非对称加密。非对称加密包括一个由公钥和私钥组成的密钥对，其中公钥用于加密，私钥用于解密。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538735195882.png" alt="1538735195882"></p> <blockquote><p>数字签名中也同样会使用公钥和私钥组成的密钥对，不过这两个密钥的用法和非对称加密是相反的，即用私钥加密相当于生成签名，而用公钥解密则相当于验证签名。请大家通过比较两张图示来理解一下“反过来用”到底是什么样的情形。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538735350386.png" alt="1538735350386"></p> <blockquote><p>那么为什么加密相当于生成签名，而解密相当于验证签名呢？要理解这个问题，我们需要回想一下非对称加密中讲过的知识，即组成密钥对的两个密钥之间存在严密的数学关系，它们是一对无法拆散的伙伴。</p> <p>用公钥加密所得到的密文，只能用与该公钥配对的私钥才能解密：同样地，用私钥加密所得到的密文，也只能用与该私钥配对的公钥才能解密。也就是说，如果用某个公钥成功解密了密文，那么就能够证明这段密文是用与该公钥配对的私钥进行加密所得到的。</p> <p>用私钥进行加密这一行为只能由持有私钥的人完成，正是基于这一事实，我们才可以将用私钥加密的密文作为签名来对待。</p> <p>由于公钥是对外公开的，因此任何人都能够用公钥进行解密，这就产生了一个很大的好处，即任何人都能够对签名进行验证。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538735674794.png" alt="1538735674794"></p> <h3 id="_7-3-数字签名的方法"><a href="#_7-3-数字签名的方法" class="header-anchor">#</a> 7.3 数字签名的方法</h3> <blockquote><p>下面我们来具体介绍两种生成和验证数字签名的方法。</p> <ul><li>直接对消息签名的方法</li> <li>对消息的散列值签名的方法</li></ul> <p>直接对消息签名的方法比较容易理解，但实际上并不会使用；对消息的散列值签名的方法稍微复杂一点，但实际中我们一般都使用这种方法。</p> <p>使用直接对消息签名的方法，需要对整个消息进行加密，非常耗时，这是因为非对称加密算法本来就非常慢。那么，我们能不能生成一条很短的数据来代替消息本身呢？这就是单向散列函数。</p> <p>于是我们不必再对整个消息进行加密（即对消息签名），而是只要先用单向散列函数求出消息的散列值，然后再将散列值进行加密（对散列值签名）就可以了。无论消息有多长，散列值永远都是这么短，因此对其进行加密（签名）是非常轻松的。</p> <p><strong>（1）Alice用单向散列函数计算消息的散列值。</strong></p> <p><strong>（2）Alice用自己的私钥对散列值进行加密。</strong></p> <div class="language- extra-class"><pre><code>  用私钥加密散列值所得到的密文就是Alice对这条散列值的签名，由于只有Alice才持有自己的私钥因此,
  
  除了Alice以外，其他人是无法生成相同的签名（密文）的。
</code></pre></div><p><strong>（3）Alice将消息和签名发送给Bob。</strong></p> <p><strong>（4）Bob用Alice的公钥对收到的签名进行解密。</strong></p> <div class="language- extra-class"><pre><code>  如果收到的签名确实是用Alice的私钥进行加密而得到的密文（签名），那么用Alice的公钥应该能够正确
  
  解密，解密的结果应该等于消息的散列值。如果收到的签名不是用Alice的私钥进行加密而得到的密文，
  
  那么就无法用Alice的公钥正确解密（解密后得到的数据看起来是随机的）。
</code></pre></div><p><strong>（5）Bob将签名解密后得到的散列值与Alice直接发送的消息的散列值进行对比。</strong></p> <div class="language- extra-class"><pre><code>   如果两者一致，则签名验证成功；如果两者不一致，则签名验证失败。
</code></pre></div><p>我们将数字签名中生成签名和验证签名的过程整理成一张时间流程图 。</p></blockquote> <div class="language- extra-class"><pre><code>				                      **Alice对消息的散列值签名, Bob验证签名**
</code></pre></div><p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538734259000.png" alt="1538734259000"></p> <div class="language- extra-class"><pre><code>				**Alice对消息的散列值签名, Bob验证签名(按时间顺序)**
</code></pre></div><p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538734363579.png" alt="1538734363579"></p> <h3 id="_7-4-通过rsa实现数字签名"><a href="#_7-4-通过rsa实现数字签名" class="header-anchor">#</a> 7.4 通过RSA实现数字签名</h3> <blockquote><p>前边章节已经介绍过了如何通过自己编写的go代码生成非对称加密算法RSA的公钥和私钥文件, 假设公钥文件的文件名为 <strong>public.pem</strong>，私钥文件对应的文件名为 <strong>private.pem</strong>。</p></blockquote> <h4 id="_7-4-1-生成数字签名"><a href="#_7-4-1-生成数字签名" class="header-anchor">#</a> 7.4.1 生成数字签名</h4> <div class="language-go extra-class"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">SignatureRSA</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">// 1. 从秘钥文件中读生成的秘钥内容</span>
	fp<span class="token punctuation">,</span> err <span class="token operator">:=</span> os<span class="token punctuation">.</span><span class="token function">Open</span><span class="token punctuation">(</span><span class="token string">&quot;private.pem&quot;</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> <span class="token boolean">nil</span><span class="token punctuation">,</span> errors<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token string">&quot;打开私钥文件 - private.pem 失败!!!&quot;</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// 2. 读文件内容</span>
	fileInfo<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> fp<span class="token punctuation">.</span><span class="token function">Stat</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	all <span class="token operator">:=</span> <span class="token function">make</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">,</span> fileInfo<span class="token punctuation">.</span><span class="token function">Size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
	<span class="token boolean">_</span><span class="token punctuation">,</span> err <span class="token operator">=</span> fp<span class="token punctuation">.</span><span class="token function">Read</span><span class="token punctuation">(</span>all<span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> <span class="token boolean">nil</span><span class="token punctuation">,</span> errors<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token string">&quot;读文件内容失败!!!&quot;</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;文件内容: &quot;</span><span class="token punctuation">,</span> <span class="token function">string</span><span class="token punctuation">(</span>all<span class="token punctuation">)</span><span class="token punctuation">)</span>
	<span class="token comment">// 3. 关闭文件</span>
	<span class="token keyword">defer</span> fp<span class="token punctuation">.</span><span class="token function">Close</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token comment">// 4. 将数据解析成pem格式的数据块</span>
	block<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> pem<span class="token punctuation">.</span><span class="token function">Decode</span><span class="token punctuation">(</span>all<span class="token punctuation">)</span>
	<span class="token comment">// 5. 解析pem数据块, 得到私钥</span>
	priv_Key<span class="token punctuation">,</span> err <span class="token operator">:=</span> x509<span class="token punctuation">.</span><span class="token function">ParsePKCS1PrivateKey</span><span class="token punctuation">(</span>block<span class="token punctuation">.</span>Bytes<span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> <span class="token boolean">nil</span><span class="token punctuation">,</span> errors<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token string">&quot;解析私钥失败!!!&quot;</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span>

	<span class="token comment">// 待签名数据</span>
	myData <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token function">byte</span><span class="token punctuation">(</span><span class="token string">&quot;渡远荆门外，来从楚国游。山随平野尽，江入大荒流。月下飞天境，云生结海楼。仍怜故乡水，万里送行舟。&quot;</span><span class="token punctuation">)</span>
	<span class="token comment">// 6. 将数据通过哈希函数生成信息摘要</span>
	myHash <span class="token operator">:=</span> sha256<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	myHash<span class="token punctuation">.</span><span class="token function">Write</span><span class="token punctuation">(</span>myData<span class="token punctuation">)</span>
	result <span class="token operator">:=</span> myHash<span class="token punctuation">.</span><span class="token function">Sum</span><span class="token punctuation">(</span><span class="token boolean">nil</span><span class="token punctuation">)</span>
	<span class="token comment">// 7. 生成签名</span>
	mySignature<span class="token punctuation">,</span> err <span class="token operator">:=</span> rsa<span class="token punctuation">.</span><span class="token function">SignPKCS1v15</span><span class="token punctuation">(</span>rand<span class="token punctuation">.</span>Reader<span class="token punctuation">,</span> priv_Key<span class="token punctuation">,</span> crypto<span class="token punctuation">.</span>SHA256<span class="token punctuation">,</span> result<span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> <span class="token boolean">nil</span><span class="token punctuation">,</span> errors<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token string">&quot;生成签名失败!!!&quot;</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span>

	<span class="token keyword">return</span> mySignature<span class="token punctuation">,</span> <span class="token boolean">nil</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="_7-4-2-验证数字签名"><a href="#_7-4-2-验证数字签名" class="header-anchor">#</a> 7.4.2 验证数字签名</h4> <div class="language-go extra-class"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">VerifyRSA</span><span class="token punctuation">(</span>src <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token builtin">error</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
	<span class="token comment">// 1. 从秘钥文件中读生成的秘钥内容</span>
	fp<span class="token punctuation">,</span> err <span class="token operator">:=</span> os<span class="token punctuation">.</span><span class="token function">Open</span><span class="token punctuation">(</span><span class="token string">&quot;public.pem&quot;</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> errors<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token string">&quot;打开公钥文件 - public.pem 失败!!!&quot;</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// 2. 读文件内容</span>
	fileInfo<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> fp<span class="token punctuation">.</span><span class="token function">Stat</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	all <span class="token operator">:=</span> <span class="token function">make</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">,</span> fileInfo<span class="token punctuation">.</span><span class="token function">Size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
	num<span class="token punctuation">,</span> err <span class="token operator">:=</span> fp<span class="token punctuation">.</span><span class="token function">Read</span><span class="token punctuation">(</span>all<span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> errors<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token string">&quot;读文件内容失败!!!&quot;</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;文件大小: &quot;</span><span class="token punctuation">,</span> num<span class="token punctuation">)</span>
	<span class="token comment">// 3. 关闭文件</span>
	<span class="token keyword">defer</span> fp<span class="token punctuation">.</span><span class="token function">Close</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token comment">// 4. 将公钥数据解析为pem格式的数据块</span>
	block<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> pem<span class="token punctuation">.</span><span class="token function">Decode</span><span class="token punctuation">(</span>all<span class="token punctuation">)</span>
	<span class="token comment">// 5. 将公钥从pem数据块中提取出来</span>
	pubInterface<span class="token punctuation">,</span> err <span class="token operator">:=</span> x509<span class="token punctuation">.</span><span class="token function">ParsePKIXPublicKey</span><span class="token punctuation">(</span>block<span class="token punctuation">.</span>Bytes<span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> errors<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token string">&quot;解析公钥失败!!!&quot;</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// 6. 公钥接口转换为公钥对象</span>
	pubKey <span class="token operator">:=</span> pubInterface<span class="token punctuation">.</span><span class="token punctuation">(</span><span class="token operator">*</span>rsa<span class="token punctuation">.</span>PublicKey<span class="token punctuation">)</span>
	<span class="token comment">// 待认证数据</span>
	myData <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token function">byte</span><span class="token punctuation">(</span><span class="token string">&quot;渡远荆门外，来从楚国游。山随平野尽，江入大荒流。月下飞天境，云生结海楼。仍怜故乡水，万里送行舟。&quot;</span><span class="token punctuation">)</span>
	<span class="token comment">// 7. 将数据通过哈希函数生成信息摘要</span>
	myHash <span class="token operator">:=</span> sha256<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	myHash<span class="token punctuation">.</span><span class="token function">Write</span><span class="token punctuation">(</span>myData<span class="token punctuation">)</span>
	result <span class="token operator">:=</span> myHash<span class="token punctuation">.</span><span class="token function">Sum</span><span class="token punctuation">(</span><span class="token boolean">nil</span><span class="token punctuation">)</span>

	<span class="token comment">// 7. 数据认证</span>
	err <span class="token operator">=</span> rsa<span class="token punctuation">.</span><span class="token function">VerifyPKCS1v15</span><span class="token punctuation">(</span>pubKey<span class="token punctuation">,</span> crypto<span class="token punctuation">.</span>SHA256<span class="token punctuation">,</span> result<span class="token punctuation">,</span> src<span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> err
	<span class="token punctuation">}</span>

	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;数字签名验证成功, 恭喜o(*￣︶￣*)o恭喜&quot;</span><span class="token punctuation">)</span>
	<span class="token keyword">return</span> <span class="token boolean">nil</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="_7-5-使用椭圆曲线实现数字签名"><a href="#_7-5-使用椭圆曲线实现数字签名" class="header-anchor">#</a> 7.5 使用椭圆曲线实现数字签名</h3> <blockquote></blockquote> <h3 id="_7-6-数字签名无法解决的问题"><a href="#_7-6-数字签名无法解决的问题" class="header-anchor">#</a> 7.6 数字签名无法解决的问题</h3> <blockquote><p>用数字签名既可以识别出篡改和伪装，还可以防止否认。也就是说，我们同时实现了确认消息的完整性、进行认证以及否认防止。现代社会中的计算机通信从这一技术中获益匪浅。</p> <p>然而，**要正确使用数字签名，有一个大前提，那是用于验证签名的公钥必须属于真正的发送者。**即便数字签名算法再强大，如果你得到的公钥是伪造的，那么数字签名也会完全失效。</p> <p>现在我们发现自己陷人了一个死循环一一一数字签名是用来识别消息篡改、伪装以及否认的，但是为此我们又必须从没有被伪装的发送者得到没有被篡改的公钥才行。</p> <p>为了能够确认自己得到的公钥是否合法，我们需要使用证书。所谓证书，就是将公钥当作一条消息，由一个可信的第三方对其签名后所得到的公钥。</p> <p>当然，这样的方法只是把问题转移了而已。为了对证书上施加的数字签名进行验证，我们必定需要另一个公钥，那么如何才能构筑一个可信的数字签名链条呢？又由谁来颁发可信的证书呢？到这一步，我们就已经踏人了社会学的领域。我们需要让公钥以及数字签名技术成为一种社会性的基础设施，即公钥基础设施（Public Key Intrastructure），简称PKIO关于证书和PKI我们将在第8章中介绍。</p></blockquote> <h2 id="_8-证书"><a href="#_8-证书" class="header-anchor">#</a> 8. 证书</h2> <div class="language-go extra-class"><pre class="language-go"><code><span class="token string">&quot;证书 -- 为公钥加上数字签名&quot;</span>
</code></pre></div><blockquote><p>要开车得先考驾照．驾照上面记有本人的照片、姓名、出生日期等个人信息．以及有效期、准驾车辆的类型等信息，并由公安局在上面盖章。我们只要看到驾照，就可以知道公安局认定此人具有驾驶车辆的资格。</p> <p>公钥证书（Public-Key Certificate，PKC)其实和驾照很相似，里面记有姓名、组织、邮箱地址等个人信息，以及属于此人的公钥，并由认证机构（Certification Authority、Certifying Authority, CA）施加数字签名。只要看到公钥证书，我们就可以知道认证机构认定该公钥的确属于此人。公钥证书也简称为证书（certificate）。</p> <p>可能很多人都没听说过认证机构，认证机构就是能够认定 “公钥确实属于此人&quot;，并能够生成数字签名的个人或者组织。认证机构中有国际性组织和政府所设立的组织，也有通过提供认证服务来盈利的一般企业，此外个人也可以成立认证机构。</p></blockquote> <h3 id="_8-1-证书的应用场景"><a href="#_8-1-证书的应用场景" class="header-anchor">#</a> 8.1 证书的应用场景</h3> <blockquote><p>下面我们来通过证书的代表性应用场景来理解证书的作用。</p> <p>下图展示了Alice向Bob发送密文的场景，在生成密文时所使用的Bob的公钥是通过认证机构获取的。</p> <p>认证机构必须是可信的，对于“可信的第三方”，下图中会使用Trent这个名字，这个词是从trust（信任）一词演变而来的。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538736605294.png" alt="1538736605294"></p> <blockquote><p>下面让我们对照着上图来看一看这些步骤具体都做了些什么。</p> <ol><li><p><font color="red" size="4">Bob生成密钥对</font></p> <p>要使用公钥密码进行通信，首先需要生成密钥对。Bob生成了一对公钥和私钥，并将私钥自行妥善保管。在这里，密钥对是由Bob自己生成的，也可以由认证机构代为生成。</p></li> <li><p><font color="red" size="4">Bob在认证机构Trent注册自己的公钥</font></p> <ul><li><p>在这里Bob则将公钥发送给了认证机构Trent，这是因为Bob需要请认证机构Trent对他的公钥加上数字签名（也就是生成证书）。</p></li> <li><p>Trent收到Bob的公钥后，会确认所收到的公钥是否为Bob本人所有（参见专栏：身份确认和认证业务准则）</p> <blockquote><p><font color="red" size="5">专栏：身份确认和认证业务准则</font></p> <p>认证机构确认&quot;本人&quot;身份的方法和认证机构的认证业务准则（CertificatePractice Statement, CPS，的内容有关。如果认证机构提供的是测试用的服务，那么可能完全不会进行任何身份确认。如果是政府部门运營的认证机构，可能就需要根据法律规定来进行身份确认。如果是企业面向内部设立的认证机构，那就可能会给部门负责人打电话直接确认。</p> <p>例如，VeriSign的认证业务准则中将身份确认分为Class1 ~ 3共三个等级</p> <ul><li>Class1：通过向邮箱发送件来确认本人身份</li> <li>Class2：通过第三方数据库来确认本人身份</li> <li>Class3：通过当面认证和身份证明来确认本人身份</li></ul> <p>等级越高，身份确认越严格。</p></blockquote></li></ul></li> <li><p><font color="red" size="4">认证机构Trent用自己的私钥对Bob的公钥施加数字签名并生成证书</font></p> <p>Trent对Bob的公钥加上数字签名。为了生成数字签名，需要Trent自身的私钥，因此Trent需要事先生成好密钥对。</p></li> <li><p><font color="red" size="4">Alice得到带有认证机构Trent的数字签名的Bob的公钥（证书）</font></p> <p>现在Alice需要向Bob发送密文，因此她从Trent处获取证书。证书中包含了Bob的公钥。</p></li> <li><p><font color="red" size="4">Alice使用认证机构Trent的公钥验证数字签名，确认Bob的公钥的合法性</font></p> <p>Alice使用认证机构Trent的公钥对证书中的数字签名进行验证。如果验证成功，就相当于确认了证书中所包含的公钥的确是属于Bob的。到这里，Alice就得到了合法的Bob的公钥。</p></li> <li><p><font color="red" size="4">Alice用Bob的公钥加密消息并发送给Bob</font></p> <p>Alice用Bob的公钥加密要发送的消息，并将消息发送给Bob。</p></li> <li><p><font color="red" size="4">Bob用自己的私钥解密密文得到Alice的消息</font></p> <p>Bob收到Alice发送的密文，然后用自己的私钥解密，这样就能够看到Alice的消息了。</p></li></ol> <p><font color="blue" size="4">上面就是利用认证机构Trent进行公钥密码通信的流程。其中1、2、3这几个步骤仅在注册新公钥时才会进行，并不是每次通信都需要。此外，步骤 4 仅在Alice第一次用公钥密码向Bob发送消息时才需要进行，只要Alice将Bob的公钥保存在电脑中，在以后的通信中就可以直接使用了。</font></p></blockquote> <h3 id="_8-2-证书标准规范x-509"><a href="#_8-2-证书标准规范x-509" class="header-anchor">#</a> 8.2 证书标准规范X.509</h3> <blockquote><p>证书是由认证机构颁发的，使用者需要对证书进行验证，因此如果证书的格式千奇百怪那就不方便了。于是，人们制定了证书的标准规范，其中使用最广泛的是由ITU（International TelecommumcationUnion，国际电信联盟）和ISO（IntemationalOrganizationforStandardization, 国际标准化组织）制定的X.509规范。很多应用程序都支持x.509并将其作为证书生成和交换的标准规范。</p> <p>X.509是一种非常通用的证书格式。所有的证书都符合ITU-T X.509国际标准，因此(理论上)为一种应用创建的证书可以用于任何其他符合X.509标准的应用。X.509证书的结构是用ASN1(Abstract Syntax Notation One)进行描述数据结构，并使用ASN.1语法进行编码。</p> <p>在一份证书中，必须证明公钥及其所有者的姓名是一致的。对X.509证书来说，认证者总是<a href="https://baike.baidu.com/item/CA" target="_blank" rel="noopener noreferrer">CA<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>或由CA指定的人，一份X.509证书是一些标准字段的集合，这些字段包含有关用户或设备及其相应公钥的信息。X.509标准定义了证书中应该包含哪些信息，并描述了这些信息是如何编码的(即数据格式)</p> <p>一般来说，一个数字证书内容可能包括基本数据（版本、序列号) 、所签名对象信息（ 签名算法类型、签发者信息、有效期、被签发人、签发的公开密钥）、CA的数字签名，等等。</p></blockquote> <h4 id="_8-2-1-证书规范"><a href="#_8-2-1-证书规范" class="header-anchor">#</a> 8.2.1 证书规范</h4> <blockquote><p>前使用最广泛的标准为ITU和ISO联合制定的X.509的 v3版本规范 (RFC5280）, 其中定义了如下证书信息域：</p> <ul><li><p><font color="red">版本号(Version Number）</font>：规范的版本号，目前为版本3，值为0x2；</p></li> <li><p><font color="red">序列号（Serial Number）</font>：由CA维护的为它所发的每个证书分配的一的列号，用来追踪和撤销证书。只要拥有签发者信息和序列号，就可以唯一标识一个证书，最大不能过20个字节；</p></li> <li><p><font color="red">签名算法（Signature Algorithm）</font>：数字签名所采用的算法，如：</p> <ul><li>sha256-with-RSA-Encryption</li> <li>ccdsa-with-SHA2S6；</li></ul></li> <li><p><font color="red">颁发者（Issuer）</font>：发证书单位的标识信息，如 ” C=CN，ST=Beijing, L=Beijing, O=org.example.com，CN=ca.org。example.com ”；</p></li> <li><p><font color="red">有效期(Validity)</font>:  证书的有效期很，包括起止时间。</p></li> <li><p><font color="red">主体(Subject)</font> : 证书拥有者的标识信息（Distinguished Name），如：&quot; C=CN，ST=Beijing, L=Beijing, CN=person.org.example.com”；</p></li> <li><p><font color="red">主体的公钥信息(SubJect Public Key Info）</font>：所保护的公钥相关的信息：</p> <ul><li>公钥算法 (Public Key Algorithm）公钥采用的算法；</li> <li>主体公钥（Subject Unique Identifier）：公钥的内容。</li></ul></li> <li><p><font color="red">颁发者唯一号（Issuer Unique Identifier）</font>：代表颁发者的唯一信息，仅2、3版本支持，可选；</p></li> <li><p><font color="red">主体唯一号（Subject Unique Identifier）</font>：代表拥有证书实体的唯一信息，仅2，3版本支持，可选：</p></li> <li><p><font color="red">扩展（Extensions，可选）</font>: 可选的一些扩展。中可能包括：</p> <ul><li>Subject Key Identifier：实体的秘钥标识符，区分实体的多对秘钥；</li> <li>Basic Constraints：一指明是否属于CA;</li> <li>Authority Key Identifier：证书颁发者的公钥标识符；</li> <li>CRL Distribution Points: 撤销文件的颁发地址；</li> <li>Key Usage：证书的用途或功能信息。</li></ul></li></ul> <p>此外，证书的颁发者还需要对证书内容利用自己的私钥添加签名， 以防止别人对证书的内容进行篡改。</p></blockquote> <h4 id="_8-2-2-证书格式"><a href="#_8-2-2-证书格式" class="header-anchor">#</a> 8.2.2 证书格式</h4> <blockquote><p>X.509规范中一般推荐使用PEM(Privacy Enhanced Mail）格式来存储证书相关的文件。证书文件的文件名后缀一般为 .crt 或 .cer 。对应私钥文件的文件名后缀一般为 .key。证书请求文件的文件名后綴为 .csr 。有时候也统一用pem作为文件名后缀。</p> <p>PEM格式采用文本方式进行存储。一般包括首尾标记和内容块，内容块采用Base64进行编码。</p> <p><strong>编码格式总结:</strong></p> <ul><li><strong>X.509 DER(Distinguished Encoding Rules)编码，后缀为：.der .cer .crt</strong></li> <li><strong>X.509 BASE64编码(PEM格式)，后缀为：.pem .cer .crt</strong></li></ul> <p>例如，一个PEM格式（base64编码）的示例证书文件内容如下所示：</p></blockquote> <div class="language-go extra-class"><pre class="language-go"><code><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span>BEGIN CERTIFICATE<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span>
MIIDyjCCArKgAwIBAgIQdZfkKrISoINLporOrZLXPTANBgkqhkiG9w0BAQsFADBn
MSswKQYDVQQLDCJDcmVhdGVkIGJ5IGh0dHA6Ly93d3cuZmlkZGxlcjIuY29tMRUw
EwYDVQQKDAxET19OT1RfVFJVU1QxITAfBgNVBAMMGERPX05PVF9UUlVTVF9GaWRk
bGVyUm9vdDAeFw0xNzA0MTExNjQ4MzhaFw0yMzA0MTExNjQ4MzhaMFoxKzApBgNV
BAsMIkNyZWF0ZWQgYnkgaHR0cDovL3d3dy5maWRkbGVyMi5jb20xFTATBgNVBAoM
DERPX05PVF9UUlVTVDEUMBIGA1UEAwwLKi5iYWlkdS5jb20wggEiMA0GCSqGSIb3
DQEBAQUAA4IBDwAwggEKAoIBAQDX0AM198jxwRoKgwWsd9oj5vI0and9v9SB9Chl
gZEu6G9ZA0C7BucsBzJ2bl0Mf6qq0Iee1DfeydfEKyTmBKTafgb2DoQE3OHZjy0B
QTJrsOdf5s636W5gJp4f7CUYYA<span class="token operator">/</span><span class="token number">3e1</span>nxr<span class="token operator">/</span><span class="token operator">+</span>AuG44Idlsi17TWodVKjsQhjzH<span class="token operator">+</span>bK6
<span class="token number">8</span>ukQZyel1SgBeQOivzxXe0rhXzrocoeKZFmUxLkUpm<span class="token operator">+</span><span class="token operator">/</span>mX1syDTdaCmQ6LT4KYYi
soKe4f<span class="token operator">+</span>r2tLbUzPKxtk2F1v3ZLOjiRdzCOA27e5n88zdAFrCmMB4teG<span class="token operator">/</span>azCSAH3g
Yb6vaAGaOnKyDLGunW51sSesWBpHceJnMfrhwxCjiv707JZtAgMBAAGjfzB9MA4G
A1UdDwEB<span class="token operator">/</span>wQEAwIEsDATBgNVHSUEDDAKBggrBgEFBQcDATAWBgNVHREEDzANggsq
LmJhaWR1LmNvbTAfBgNVHSMEGDAWgBQ9UIffUQSuwWGOm<span class="token operator">+</span>o74JffZJNadjAdBgNV
HQ4EFgQUQh8IksZqcMVmKrIibTHLbAgLRGgwDQYJKoZIhvcNAQELBQADggEBAC5Y
JndwXpm0W<span class="token operator">+</span><span class="token number">9</span>SUlQhAUSE9LZh<span class="token operator">+</span>DzcSmlCWtBk<span class="token operator">+</span>SKBwmAegbfNSf6CgCh0VY6iIhbn
GlszqgAOAqVMxAEDlR<span class="token operator">/</span>YJTOlAUXFw8KICsWdvE01xtHqhk1tCK154Otci60Wu<span class="token operator">+</span>tz
<span class="token number">1</span>t8999GPbJskecbRDGRDSA<span class="token operator">/</span>gQGZJuL0rnmIuz3macSVn6tH7NwdoNeN68Uj3Qyt5
orYv1IFm8t55224ga8ac1y90hK4R5HcvN71aIjMKrikgynK0E<span class="token operator">+</span>g45QypHRIe<span class="token operator">/</span>z0S
<span class="token operator">/</span><span class="token number">1</span>W<span class="token operator">/</span><span class="token number">6</span>rqTgfN6OWc0c15hPeJbTtkntB5Fqd0sfsnKkW6jPsKQ<span class="token operator">+</span>z<span class="token operator">/</span><span class="token operator">+</span>vZ5XqzdlFupQ
<span class="token number">29</span>F14ei8ZHl9aLIHP5s<span class="token operator">=</span>
<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span>END CERTIFICATE<span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span>
</code></pre></div><p>证书中的解析出来的内容：</p> <div class="language-go extra-class"><pre class="language-go"><code>Certificate<span class="token punctuation">:</span>
    Data<span class="token punctuation">:</span>
        Version<span class="token punctuation">:</span> <span class="token number">3</span> <span class="token punctuation">(</span><span class="token number">0x2</span><span class="token punctuation">)</span>
        Serial Number<span class="token punctuation">:</span>
            <span class="token number">10</span><span class="token punctuation">:</span>e6<span class="token punctuation">:</span>fc<span class="token punctuation">:</span><span class="token number">62</span><span class="token punctuation">:</span>b7<span class="token punctuation">:</span><span class="token number">41</span><span class="token punctuation">:</span><span class="token number">8</span>a<span class="token punctuation">:</span>d5<span class="token punctuation">:</span><span class="token number">00</span><span class="token punctuation">:</span><span class="token number">5</span>e<span class="token punctuation">:</span><span class="token number">45</span><span class="token punctuation">:</span>b6
    Signature Algorithm<span class="token punctuation">:</span> sha256WithRSAEncryption
        Issuer<span class="token punctuation">:</span> C<span class="token operator">=</span>BE<span class="token punctuation">,</span> O<span class="token operator">=</span>GlobalSign nv<span class="token operator">-</span>sa<span class="token punctuation">,</span> CN<span class="token operator">=</span>GlobalSign Organization Validation CA<span class="token operator">-</span>SHA256<span class="token operator">-</span>G2
        Validity
            Not Before<span class="token punctuation">:</span> Nov <span class="token number">21</span> <span class="token number">08</span><span class="token punctuation">:</span><span class="token number">00</span><span class="token punctuation">:</span><span class="token number">00</span> <span class="token number">2016</span> GMT
            Not After <span class="token punctuation">:</span> Nov <span class="token number">22</span> <span class="token number">07</span><span class="token punctuation">:</span><span class="token number">59</span><span class="token punctuation">:</span><span class="token number">59</span> <span class="token number">2017</span> GMT
        Subject<span class="token punctuation">:</span> C<span class="token operator">=</span>US<span class="token punctuation">,</span> ST<span class="token operator">=</span>California<span class="token punctuation">,</span> L<span class="token operator">=</span>San Francisco<span class="token punctuation">,</span> O<span class="token operator">=</span>Wikimedia Foundation<span class="token punctuation">,</span> Inc<span class="token punctuation">.</span><span class="token punctuation">,</span> CN<span class="token operator">=</span><span class="token operator">*</span><span class="token punctuation">.</span>wikipedia<span class="token punctuation">.</span>org
        Subject Public Key Info<span class="token punctuation">:</span>
            Public Key Algorithm<span class="token punctuation">:</span> id<span class="token operator">-</span>ecPublicKey
                Public<span class="token operator">-</span>Key<span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token number">256</span> bit<span class="token punctuation">)</span>
                pub<span class="token punctuation">:</span> 
                    <span class="token number">04</span><span class="token punctuation">:</span>c9<span class="token punctuation">:</span><span class="token number">22</span><span class="token punctuation">:</span><span class="token number">69</span><span class="token punctuation">:</span><span class="token number">31</span><span class="token punctuation">:</span><span class="token number">8</span>a<span class="token punctuation">:</span>d6<span class="token punctuation">:</span><span class="token number">6</span>c<span class="token punctuation">:</span>ea<span class="token punctuation">:</span>da<span class="token punctuation">:</span>c3<span class="token punctuation">:</span><span class="token number">7</span>f<span class="token punctuation">:</span><span class="token number">2</span>c<span class="token punctuation">:</span>ac<span class="token punctuation">:</span>a5<span class="token punctuation">:</span>
                    af<span class="token punctuation">:</span>c0<span class="token punctuation">:</span><span class="token number">02</span><span class="token punctuation">:</span>ea<span class="token punctuation">:</span><span class="token number">81</span><span class="token punctuation">:</span>cb<span class="token punctuation">:</span><span class="token number">65</span><span class="token punctuation">:</span>b9<span class="token punctuation">:</span>fd<span class="token punctuation">:</span><span class="token number">0</span>c<span class="token punctuation">:</span><span class="token number">6</span>d<span class="token punctuation">:</span><span class="token number">46</span><span class="token punctuation">:</span><span class="token number">5</span>b<span class="token punctuation">:</span>c9<span class="token punctuation">:</span><span class="token number">1</span>e<span class="token punctuation">:</span>
                    ed<span class="token punctuation">:</span>b2<span class="token punctuation">:</span>ac<span class="token punctuation">:</span><span class="token number">2</span>a<span class="token punctuation">:</span><span class="token number">1</span>b<span class="token punctuation">:</span><span class="token number">4</span>a<span class="token punctuation">:</span>ec<span class="token punctuation">:</span><span class="token number">80</span><span class="token punctuation">:</span><span class="token number">7</span>b<span class="token punctuation">:</span>e7<span class="token punctuation">:</span><span class="token number">1</span>a<span class="token punctuation">:</span><span class="token number">51</span><span class="token punctuation">:</span>e0<span class="token punctuation">:</span>df<span class="token punctuation">:</span>f7<span class="token punctuation">:</span>
                    c7<span class="token punctuation">:</span><span class="token number">4</span>a<span class="token punctuation">:</span><span class="token number">20</span><span class="token punctuation">:</span><span class="token number">7</span>b<span class="token punctuation">:</span><span class="token number">91</span><span class="token punctuation">:</span><span class="token number">4</span>b<span class="token punctuation">:</span><span class="token number">20</span><span class="token punctuation">:</span><span class="token number">07</span><span class="token punctuation">:</span><span class="token number">21</span><span class="token punctuation">:</span>ce<span class="token punctuation">:</span>cf<span class="token punctuation">:</span><span class="token number">68</span><span class="token punctuation">:</span><span class="token number">65</span><span class="token punctuation">:</span><span class="token number">8</span>c<span class="token punctuation">:</span>c6<span class="token punctuation">:</span>
                    <span class="token number">9</span>d<span class="token punctuation">:</span><span class="token number">3</span>b<span class="token punctuation">:</span>ef<span class="token punctuation">:</span>d5<span class="token punctuation">:</span>c1
                ASN1 OID<span class="token punctuation">:</span> prime256v1
                NIST CURVE<span class="token punctuation">:</span> P<span class="token operator">-</span><span class="token number">256</span>
        X509v3 extensions<span class="token punctuation">:</span>
            X509v3 Key Usage<span class="token punctuation">:</span> critical
                Digital Signature<span class="token punctuation">,</span> Key Agreement
            Authority Information Access<span class="token punctuation">:</span> 
                CA Issuers <span class="token operator">-</span> URI<span class="token punctuation">:</span>http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>secure<span class="token punctuation">.</span>globalsign<span class="token punctuation">.</span>com<span class="token operator">/</span>cacert<span class="token operator">/</span>gsorganizationvalsha2g2r1<span class="token punctuation">.</span>crt
                OCSP <span class="token operator">-</span> URI<span class="token punctuation">:</span>http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>ocsp2<span class="token punctuation">.</span>globalsign<span class="token punctuation">.</span>com<span class="token operator">/</span>gsorganizationvalsha2g2

            X509v3 Certificate Policies<span class="token punctuation">:</span> 
                Policy<span class="token punctuation">:</span> <span class="token number">1.3</span><span class="token number">.6</span><span class="token number">.1</span><span class="token number">.4</span><span class="token number">.1</span><span class="token number">.4146</span><span class="token number">.1</span><span class="token number">.20</span>
                  CPS<span class="token punctuation">:</span> https<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>www<span class="token punctuation">.</span>globalsign<span class="token punctuation">.</span>com<span class="token operator">/</span>repository<span class="token operator">/</span>
                Policy<span class="token punctuation">:</span> <span class="token number">2.23</span><span class="token number">.140</span><span class="token number">.1</span><span class="token number">.2</span><span class="token number">.2</span>

            X509v3 Basic Constraints<span class="token punctuation">:</span> 
                CA<span class="token punctuation">:</span>FALSE
            X509v3 CRL Distribution Points<span class="token punctuation">:</span> 

                Full Name<span class="token punctuation">:</span>
                  URI<span class="token punctuation">:</span>http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>crl<span class="token punctuation">.</span>globalsign<span class="token punctuation">.</span>com<span class="token operator">/</span>gs<span class="token operator">/</span>gsorganizationvalsha2g2<span class="token punctuation">.</span>crl

            X509v3 Subject Alternative Name<span class="token punctuation">:</span> 
                DNS<span class="token punctuation">:</span><span class="token operator">*</span><span class="token punctuation">.</span>wikipedia<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span><span class="token operator">*</span><span class="token punctuation">.</span>m<span class="token punctuation">.</span>mediawiki<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span><span class="token operator">*</span><span class="token punctuation">.</span>m<span class="token punctuation">.</span>wikibooks<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span><span class="token operator">*</span><span class="token punctuation">.</span>m<span class="token punctuation">.</span>wikidata<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span><span class="token operator">*</span><span class="token punctuation">.</span>m<span class="token punctuation">.</span>wikimedia<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span><span class="token operator">*</span><span class="token punctuation">.</span>m<span class="token punctuation">.</span>wikimediafoundation<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span><span class="token operator">*</span><span class="token punctuation">.</span>m<span class="token punctuation">.</span>wikinews<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span><span class="token operator">*</span><span class="token punctuation">.</span>m<span class="token punctuation">.</span>wikipedia<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span><span class="token operator">*</span><span class="token punctuation">.</span>m<span class="token punctuation">.</span>wikiquote<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span><span class="token operator">*</span><span class="token punctuation">.</span>m<span class="token punctuation">.</span>wikisource<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span><span class="token operator">*</span><span class="token punctuation">.</span>m<span class="token punctuation">.</span>wikiversity<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span><span class="token operator">*</span><span class="token punctuation">.</span>m<span class="token punctuation">.</span>wikivoyage<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span><span class="token operator">*</span><span class="token punctuation">.</span>m<span class="token punctuation">.</span>wiktionary<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span><span class="token operator">*</span><span class="token punctuation">.</span>mediawiki<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span><span class="token operator">*</span><span class="token punctuation">.</span>planet<span class="token punctuation">.</span>wikimedia<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span><span class="token operator">*</span><span class="token punctuation">.</span>wikibooks<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span><span class="token operator">*</span><span class="token punctuation">.</span>wikidata<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span><span class="token operator">*</span><span class="token punctuation">.</span>wikimedia<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span><span class="token operator">*</span><span class="token punctuation">.</span>wikimediafoundation<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span><span class="token operator">*</span><span class="token punctuation">.</span>wikinews<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span><span class="token operator">*</span><span class="token punctuation">.</span>wikiquote<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span><span class="token operator">*</span><span class="token punctuation">.</span>wikisource<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span><span class="token operator">*</span><span class="token punctuation">.</span>wikiversity<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span><span class="token operator">*</span><span class="token punctuation">.</span>wikivoyage<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span><span class="token operator">*</span><span class="token punctuation">.</span>wiktionary<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span><span class="token operator">*</span><span class="token punctuation">.</span>wmfusercontent<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span><span class="token operator">*</span><span class="token punctuation">.</span>zero<span class="token punctuation">.</span>wikipedia<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span>mediawiki<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span>w<span class="token punctuation">.</span>wiki<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span>wikibooks<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span>wikidata<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span>wikimedia<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span>wikimediafoundation<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span>wikinews<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span>wikiquote<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span>wikisource<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span>wikiversity<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span>wikivoyage<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span>wiktionary<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span>wmfusercontent<span class="token punctuation">.</span>org<span class="token punctuation">,</span> DNS<span class="token punctuation">:</span>wikipedia<span class="token punctuation">.</span>org
            X509v3 Extended Key Usage<span class="token punctuation">:</span> 
                TLS Web Server Authentication<span class="token punctuation">,</span> TLS Web Client Authentication
            X509v3 Subject Key Identifier<span class="token punctuation">:</span> 
                <span class="token number">28</span><span class="token punctuation">:</span><span class="token number">2</span>A<span class="token punctuation">:</span><span class="token number">26</span><span class="token punctuation">:</span><span class="token number">2</span>A<span class="token punctuation">:</span><span class="token number">57</span><span class="token punctuation">:</span><span class="token number">8</span>B<span class="token punctuation">:</span><span class="token number">3</span>B<span class="token punctuation">:</span>CE<span class="token punctuation">:</span>B4<span class="token punctuation">:</span>D6<span class="token punctuation">:</span>AB<span class="token punctuation">:</span><span class="token number">54</span><span class="token punctuation">:</span>EF<span class="token punctuation">:</span>D7<span class="token punctuation">:</span><span class="token number">38</span><span class="token punctuation">:</span><span class="token number">21</span><span class="token punctuation">:</span><span class="token number">2</span>C<span class="token punctuation">:</span><span class="token number">49</span><span class="token punctuation">:</span><span class="token number">5</span>C<span class="token punctuation">:</span><span class="token number">36</span>
            X509v3 Authority Key Identifier<span class="token punctuation">:</span> 
                keyid<span class="token punctuation">:</span><span class="token number">96</span><span class="token punctuation">:</span>DE<span class="token punctuation">:</span><span class="token number">61</span><span class="token punctuation">:</span>F1<span class="token punctuation">:</span>BD<span class="token punctuation">:</span><span class="token number">1</span>C<span class="token punctuation">:</span><span class="token number">16</span><span class="token punctuation">:</span><span class="token number">29</span><span class="token punctuation">:</span><span class="token number">53</span><span class="token punctuation">:</span><span class="token number">1</span>C<span class="token punctuation">:</span>C0<span class="token punctuation">:</span>CC<span class="token punctuation">:</span><span class="token number">7</span>D<span class="token punctuation">:</span><span class="token number">3</span>B<span class="token punctuation">:</span><span class="token number">83</span><span class="token punctuation">:</span><span class="token number">00</span><span class="token punctuation">:</span><span class="token number">40</span><span class="token punctuation">:</span>E6<span class="token punctuation">:</span><span class="token number">1</span>A<span class="token punctuation">:</span><span class="token number">7</span>C

    Signature Algorithm<span class="token punctuation">:</span> sha256WithRSAEncryption
         <span class="token number">8</span>b<span class="token punctuation">:</span>c3<span class="token punctuation">:</span>ed<span class="token punctuation">:</span>d1<span class="token punctuation">:</span><span class="token number">9</span>d<span class="token punctuation">:</span><span class="token number">39</span><span class="token punctuation">:</span><span class="token number">6</span>f<span class="token punctuation">:</span>af<span class="token punctuation">:</span><span class="token number">40</span><span class="token punctuation">:</span><span class="token number">72</span><span class="token punctuation">:</span>bd<span class="token punctuation">:</span><span class="token number">1</span>e<span class="token punctuation">:</span><span class="token number">18</span><span class="token punctuation">:</span><span class="token number">5</span>e<span class="token punctuation">:</span><span class="token number">30</span><span class="token punctuation">:</span><span class="token number">54</span><span class="token punctuation">:</span><span class="token number">23</span><span class="token punctuation">:</span><span class="token number">35</span><span class="token punctuation">:</span>
         <span class="token operator">...</span>
</code></pre></div><h4 id="_8-2-3-ca证书"><a href="#_8-2-3-ca证书" class="header-anchor">#</a> 8.2.3 CA证书</h4> <blockquote><p>证书是用来证明某某东西确实是某某东西的东西（是不是像绕口令？）。通俗地说，证书就好比上文里面的公章。通过公章，可以证明对应的证件的真实性。</p> <p>理论上，人人都可以找个证书工具，自己做一个证书。那如何防止坏人自己制作证书出来骗人捏？请看后续 CA 的介绍。</p> <p>CA是Certificate Authority的缩写，也叫“证书授权中心”。</p> <p>它是负责管理和签发证书的第三方机构, 好比一个可信任的中介公司。一般来说，CA必须是所有行业和所有公众都信任的、认可的。因此它必须具有足够的权威性。就好比A、B两公司都必须信任C公司，才会找 C 公司作为公章的中介。</p></blockquote> <ul><li><p>CA证书</p> <blockquote><p>CA 证书，顾名思义，就是CA颁发的证书。</p> <p>前面已经说了，人人都可以找工具制作证书。但是你一个小破孩制作出来的证书是没啥用处的。因为你不是权威的CA机关，你自己搞的证书不具有权威性。</p> <p>比如，某个坏人自己刻了一个公章，盖到介绍信上。但是别人一看，不是受信任的中介公司的公章，就不予理睬。坏蛋的阴谋就不能得逞啦。</p></blockquote></li> <li><p>证书信任链</p> <blockquote><p>证书直接是可以有信任关系的, 通过一个证书可以证明另一个证书也是真实可信的. 实际上，证书之间的信任关系，是可以嵌套的。比如，C 信任 A1，A1 信任 A2，A2 信任 A3......这个叫做证书的信任链。只要你信任链上的头一个证书，那后续的证书，都是可以信任滴。</p> <p>假设 C 证书信任 A 和 B；然后 A 信任 A1 和 A2；B 信任 B1 和 B2。则它们之间，构成如下的一个树形关系（一个倒立的树）。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1533293270573.png" alt="1533293270573"></p> <blockquote><p>处于最顶上的树根位置的那个证书，就是“<strong>根证书</strong>”。除了根证书，其它证书都要依靠上一级的证书，来证明自己。那谁来证明“根证书”可靠捏？实际上，根证书自己证明自己是可靠滴（或者换句话说，根证书是不需要被证明滴）。</p> <p>聪明的同学此刻应该意识到了：根证书是整个证书体系安全的根本。所以，如果某个证书体系中，根证书出了问题（不再可信了），那么所有被根证书所信任的其它证书，也就不再可信了。</p></blockquote></li> <li><p>证书有啥用</p> <blockquote><ol><li><p><strong>验证网站是否可信（针对HTTPS）</strong></p> <p>通常，我们如果访问某些敏感的网页（比如用户登录的页面），其协议都会使用 HTTPS 而不是 HTTP。因为 HTTP 协议是明文的，一旦有坏人在偷窥你的网络通讯，他/她就可以看到网络通讯的内容（比如你的密码、银行帐号、等）；而 HTTPS 是加密的协议，可以保证你的传输过程中，坏蛋无法偷窥。</p> <p>但是，千万不要以为，HTTPS 协议有了加密，就可高枕无忧了。俺再举一个例子来说明，光有加密是不够滴。假设有一个坏人，搞了一个假的网银的站点，然后诱骗你上这个站点。假设你又比较单纯，一不留神，就把你的帐号，口令都输入进去了。那这个坏蛋的阴谋就得逞鸟。</p> <p>为了防止坏人这么干，HTTPS 协议除了有加密的机制，还有一套证书的机制。通过证书来确保，某个站点确实就是某个站点。</p> <p>有了证书之后，当你的浏览器在访问某个 HTTPS 网站时，会验证该站点上的 CA 证书（类似于验证介绍信的公章）。如果浏览器发现该证书没有问题（证书被某个根证书信任、证书上绑定的域名和该网站的域名一致、证书没有过期），那么页面就直接打开；否则的话，浏览器会给出一个警告，告诉你该网站的证书存在某某问题，是否继续访问该站点？下面给出 IE 和 Firefox 的抓图：</p> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/03113237-5408b6ece0c84744b106e639ddf4956b.png" alt="img"></p> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/03113237-1369f659d6e4496e8bb3a742198cef63.png" alt="img"></p></li></ol></blockquote> <p><img src="http://qiniu.zhequtao.com/WechatIMG386.png" alt="img"></p> <blockquote><p>大多数知名的网站，如果用了 HTTPS 协议，其证书都是可信的（也就不会出现上述警告）。所以，今后你如果上某个知名网站，发现浏览器跳出上述警告，你就要小心啦！</p> <ol start="2"><li><p><strong>验证某文件是否可信（是否被篡改）</strong></p> <p>证书除了可以用来验证某个网站，还可以用来验证某个文件是否被篡改。具体是通过证书来制作文件的数字签名。制作数字签名的过程太专业，咱就不说了。后面专门告诉大家如何验证文件的数字签名。考虑到大多数人用 Windows 系统，俺就拿 Windows 的例子来说事儿。</p> <p>比如，俺手头有一个 Google Chrome的安装文件（带有数字签名）。当俺查看该文件的属性，会看到如下的界面。眼神好的同学，会注意到到上面有个“<strong>数字签名</strong>”的标签页。如果没有出现这个标签页，就说明该文件没有附带数字签名。</p> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1533294208392.png" alt="1533294208392"></p> <p>一般来说，签名列表中，有且仅有一个签名。选中它，点“<strong>详细信息</strong>”按钮。跳出如下界面：</p> <p>通常这个界面会显示一行字：“<strong>该数字签名正常</strong>”（图中红圈标出）。如果有这行字，就说明该文件从出厂到你手里，中途没有被篡改过（是原装滴、是纯洁滴）。如果该文件被篡改过了（比如，感染了病毒、被注入木马），那么对话框会出现一个警告提示“<strong>该数字签名无效</strong>”</p> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1533294414623.png" alt="1533294414623"></p> <p>不论签名是否正常，你都可以点“查看证书”按钮。这时候，会跳出证书的对话框。如下：</p> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1533294685323.png" alt="1533294685323"></p> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1533294711691.png" alt="1533294711691"></p> <p>从后一个界面，可以看到刚才说的证书信任链。图中的信任链有3层：</p> <ul><li>第1层是根证书（verisign）。</li> <li>第2层是 symantec 专门用来签名的证书。</li> <li>第3层是 Google自己的证书。</li></ul> <p>目前大多数知名的公司（或组织机构），其发布的可执行文件（比如软件安装包、驱动程序、安全补丁），都带有数字签名。你可以自己去看一下。</p> <p>建议大伙儿在安装软件之前，都先看看是否有数字签名？如果有，就按照上述步骤验证一把。一旦数字签名是坏的，那可千万别装。</p></li></ol></blockquote></li></ul> <h3 id="_8-3-公钥基础设施（pki）"><a href="#_8-3-公钥基础设施（pki）" class="header-anchor">#</a> 8.3 公钥基础设施（PKI）</h3> <blockquote><p>仅制定证书的规范还不足以支持公钥的实际运用，我们还需要很多其他的规范，例如证书应该由谁来颁发，如何颁发，私钥泄露时应该如何作废证书，计算机之间的数据交换应采用怎样的格式等。这一节我们将介绍能够使公钥的运用更加有效的公钥基础设施。</p></blockquote> <h4 id="_8-3-1-什么是公钥基础设施"><a href="#_8-3-1-什么是公钥基础设施" class="header-anchor">#</a> 8.3.1 什么是公钥基础设施</h4> <blockquote><p>公钥基础设施（Public-Key infrastructure）是为了能够更有效地运用公钥而制定的一系列规范和规格的总称。公钥基础设施一般根据其英语缩写而简称为PKI。</p> <p>PKI只是一个总称，而并非指某一个单独的规范或规格。例如，RSA公司所制定的PKCS（Public-Key Cryptography Standards，公钥密码标准）系列规范也是PKI的一种，而互联网规格RFC（Requestfor Comments）中也有很多与PKI相关的文档。此外，X.509这样的规范也是PKI的一种。在开发PKI程序时所使用的由各个公司编写的API（Application Programming Interface, 应用程序编程接口）和规格设计书也可以算是PKI的相关规格。</p> <p>因此，根据具体所采用的规格，PKI也会有很多变种，这也是很多人难以整体理解PKI的原因之一。</p> <p>为了帮助大家整体理解PKI,我们来简单总结一下PKI的基本组成要素（用户、认证机构、仓库）以及认证机构所负责的工作。</p></blockquote> <h4 id="_8-3-2-pki的组成要素"><a href="#_8-3-2-pki的组成要素" class="header-anchor">#</a> 8.3.2 PKI的组成要素</h4> <p>PKI的组成要素主要有以下三个：</p> <ul><li>用户 --- 使用PKI的人</li> <li>认证机构 --- 颁发证书的人</li> <li>仓库 --- 保存证书的数据库</li></ul> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538758063013.png" alt="1538758063013"></p> <h5 id="用户"><a href="#用户" class="header-anchor">#</a> 用户</h5> <blockquote><p>用户就是像Alice、Bob这样使用PKI的人。用户包括两种：一种是希望使用PKI注册自己的公钥的人，另一种是希望使用已注册的公钥的人。我们来具体看一下这两种用户所要进行的操作。</p></blockquote> <ul><li><p><strong>注册公钥的用户所进行的操作</strong></p> <ul><li>生成密钥对（也可以由认证机构生成）</li> <li>在认证机构注册公钥</li> <li>向认证机构申请证书</li> <li>根据需要申请作废已注册的公钥</li> <li>解密接收到的密文</li> <li>对消息进行数字签名</li></ul></li> <li><p><strong>使用已注册公钥的用户所进行的操作</strong></p> <ul><li>将消息加密后发送给接收者</li> <li>验证数字签名</li></ul> <div class="language-c++ extra-class"><pre class="language-text"><code>/* 
==================== 小知识点 ==================== 
浏览器如何验证SSL证书
1. 在IE浏览器的菜单中点击“工具 /Internet选项”，选择“内容”标签，点击“证书”按钮，然后就可以看到IE
   浏览器已经信任了许多“中级证书颁发机构”和“受信任的根证书颁发机 构。当我们在访问该网站时，浏览器
   就会自动下载该网站的SSL证书，并对证书的安全性进行检查。
2. 由于证书是分等级的，网站拥有者可能从根证书颁发机构领到证书，也可能从根证书的下一级（如某个国家
   的认证中心，或者是某个省发出的证书）领到证书。假设我们正在访问某个使用 了 SSL技术的网站，IE浏
   览器就会收到了一个SSL证书，如果这个证书是由根证书颁发机构签发的，IE浏览器就会按照下面的步骤来
   检查：浏览器使用内 置的根证书中的公钥来对收到的证书进行认证，如果一致，就表示该安全证书是由可信
   任的颁证机构签发的，这个网站就是安全可靠的；如果该SSL证书不是根服 务器签发的，浏览器就会自动检
   查上一级的发证机构，直到找到相应的根证书颁发机构，如果该根证书颁发机构是可信的，这个网站的SSL证
   书也是可信的。
*/
</code></pre></div></li></ul> <h5 id="认证机构（ca）"><a href="#认证机构（ca）" class="header-anchor">#</a> 认证机构（CA）</h5> <blockquote><p>认证机构（Certification Authority，CA）是对证书进行管理的人。上面的图中我们给它起了一个名字叫作Trent。认证机构具体所进行的操作如下：</p></blockquote> <ul><li><p><strong>生成密钥对 (也可以由用户生成)</strong></p> <blockquote><p>生成密钥对有两种方式：一种是由PKI用户自行生成，一种是由认证机构来生成。在认证机构生成用户密钥对的情况下，认证机构需要将私钥发送给用户，这时就需要使用PKCS#12（Personal Information Exchange Syntax Standard）等规范。</p></blockquote></li> <li><p>在注册公钥时对本人身份进行认证, 生成并颁发证书</p> <blockquote><p>在用户自行生成密钥对的情况下，用户会请求认证机构来生成证书。申请证书时所使用的规范是由PKCS#10（Certification Request Syntax Standard）定义的。</p> <p>认证机构根据其认证业务准则（Certification Practice Statement，CPS）对用户的身份进行认证，并生成证书。在生成证书时，需要使用认证机构的私钥来进行数字签名。生成的证书格式是由PKCS#6 （Extended-Certificate Syntax Standard）和 X.509定义的。</p></blockquote></li> <li><p>作废证书</p> <blockquote><p>当用户的私钥丢失、被盗时，认证机构需要对证书进行作废（revoke）。此外，即便私钥安然无恙，有时候也需要作废证书，例如用户从公司离职导致其失去私钥的使用权限，或者是名称变更导致和证书中记载的内容不一致等情况。</p> <p>纸质证书只要撕毁就可以作废了，但这里的证书是数字信息，即便从仓库中删除也无法作废，因为用户会保存证书的副本，但认证机构又不能人侵用户的电脑将副本删除。</p> <p>要作废证书，认证机构需要制作一张证书==<strong>作废清单（Certificate Revocation List),简称为CRL</strong>==。</p> <p>CRL是认证机构宣布作废的证书一览表，具体来说，是一张已作废的证书序列号的清单，并由认证机构加上数字签名。证书序列号是认证机构在颁发证书时所赋予的编号，在证书中都会记载。</p> <p>PKI用户需要从认证机构获取最新的CRL,并查询自己要用于验证签名（或者是用于加密）的公钥证书是否已经作废这个步骤是非常重要的。</p> <p>假设我们手上有Bob的证书，该证书有合法的认证机构签名，而且也在有效期内，但仅凭这些还不能说明该证书一定是有效的，还需要查询认证机构最新的CRL，并确认该证书是否有效。一般来说，这个检查不是由用户自身来完成的，而是应该由处理该证书的软件来完成，但有很多软件并没有及时更能CRL。</p></blockquote></li></ul> <blockquote><p>认证机构的工作中，公钥注册和本人身份认证这一部分可以由注册机构（Registration Authority，RA) 来分担。这样一来，认证机构就可以将精力集中到颁发证书上，从而减轻了认证机构的负担。不过，引入注册机构也有弊端，比如说认证机构需要对注册机构本身进行认证，而且随着组成要素的增加，沟通过程也会变得复杂，容易遭受攻击的点也会增。</p></blockquote> <h5 id="仓库"><a href="#仓库" class="header-anchor">#</a> 仓库</h5> <blockquote><p>仓库（repository）是一个保存证书的数据库，PKI用户在需要的时候可以从中获取证书．它的作用有点像打电话时用的电话本。在本章开头的例子中，尽管没特别提到，但Alice获取Bob的证书时，就可以使用仓库。仓库也叫作证书目录。</p></blockquote> <h4 id="_8-3-3-各种各样的pki"><a href="#_8-3-3-各种各样的pki" class="header-anchor">#</a> 8.3.3 各种各样的PKI</h4> <blockquote><p>公钥基础设施（PKI）这个名字总会引起一些误解，比如说“面向公众的权威认证机构只有一个&quot;，或者“全世界的公钥最终都是由一个根CA来认证的&quot;，其实这些都是不正确的。认证机构只要对公钥进行数字签名就可以了，因此任何人都可以成为认证机构，实际上世界上已经有无数个认证机构了。</p> <p>国家、地方政府、医院、图书馆等公共组织和团体可以成立认证机构来实现PKI,公司也可以出于业务需要在内部实现PKI,甚至你和你的朋友也可以以实验为目的来构建PKI。</p> <p>在公司内部使用的情况下，认证机构的层级可以像上一节中一样和公司的组织层级一一对应，也可以不一一对应。例如，如果公司在东京、大阪、北海道和九州都成立了分公司，也可以采取各个分公司之间相互认证的结构。在认证机构的运营方面，可以购买用于构建PKI的软件产品由自己公司运营，也可以使用VeriSign等外部认证服务。具体要采取怎样的方式，取决于目的和规模，并没有一定之规。</p></blockquote> <h2 id="_9-ssl-tls"><a href="#_9-ssl-tls" class="header-anchor">#</a> 9. SSL/TLS</h2> <div class="language-go extra-class"><pre class="language-go"><code><span class="token string">&quot;SSL/TLS --- 为了更安全的通信&quot;</span>
</code></pre></div><blockquote><p>本章中我们将学习SSL/TLS的相关知识。</p> <p>SSL/TLS是世界上应用最广泛的密码通信方法。比如说，当在网上商城中输人信用卡号时，我们的Web浏览器就会使用SSL/TLS进行密码通信。使用SSL/TLS可以对通信对象进行认证，还可以确保通信内容的机密性。</p> <p>SSL/TLS中综合运用了之前所学习的对称密码、消息认证码、公钥密码、数字签名、伪随机数生成器等密码技术，大家可以在阅读本章内容的同时对这些技术进行复习。严格来说，SSL（Secure Socket Layer)与TLS（Transport Layer Security）是不同的，TLS相当于是SSL的后续版本。不过，本章中所介绍的内容，大多是SSL和TLS两者兼备的，因此除具体介绍通信协议的部分以外，都统一写作SSL/TLS。</p></blockquote> <h3 id="_9-1-客户端与服务器"><a href="#_9-1-客户端与服务器" class="header-anchor">#</a> 9.1 客户端与服务器</h3> <blockquote><p>Bob书店是Alice经常光顾的一家网店，因为在Bob书店她可以搜索到新出版的图书，还可以通过信用卡快速完成支付，购买的书还能快递到家，真的很方便。</p> <p>有一天，Alice 读了一本关于网络信息安全的书，书上说“互联网上传输的数据都是可以被窃听的&quot;。Alice感到非常担心，自己在购买新书的时候输人的信用卡号会不会被窃听呢？</p> <p>Alice看到Bob书店的网站下面写着一行字：“在以https://开头的网页中输人的信息将通过SSL/TLS发送以确保安全&quot;。</p> <p>的确，输人信用卡号的网页的URL是以 <strong>https://</strong> 开头的，而不是一般的 http://。此外．在浏览这个网页时，Alice的web浏览器上还会显示一个小锁头的图标，看上去好像挺安全的。</p> <p>但Alice心想，就算写着“通过SSL/TLS发送”我也不放心啊，到底在我的Web浏览器和Bob书店的网站之间都发生了哪些事呢？</p> <p>本章将要介绍的技术一一SSL/TLS就可以解答Alice的疑问。当进行SSL/TLS通信时，Web浏览器上就会显示一个小锁头的图标。</p></blockquote> <div class="language- extra-class"><pre><code>                          **Alice的Web浏览器（客户端）和Bob书店的网站（服务器）进行HTTP通信**
</code></pre></div><p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538753959418.png" alt="1538753959418"></p> <blockquote><p>Alice和Bob书店之间的通信，实际上是Alice所使用的Web浏览器和Bob书店的Web服务器之间的通信。Web浏览器是Alice的计算机上运行的一个程序，而web服务器则是在Bob书店的计算机上运行的一个程序，它们都遵循一种叫作HTTP（Hyper Text Transfer Protocol, 超文本传输协议）的协议（protocol)来进行通信。其中，Web浏览器称为HTTP客户端，Web服务器称为HTTP服务器。</p> <p>当Alice点击网页上的链接或者输人URL时，Web浏览器就会通过网络向Web服务器发送一个 “我要浏览这个网页“，的请求（request）。</p> <p>Web服务器则将请求的网页内容发送给Web浏览器，以便对请求作出响应（response）。服务器和客户端之间所进行的处理就是请求和响应的往复。HTTP可以认为是在HTTP客户端与HTTP服务器之间进行请求和响应的规范。</p> <p>Alice向Bob书店发送信用卡号也是使用HTTP来完成的（下图）。Alice输人信用卡号之后按下提交按钮，这时客户端（Web浏览器）就会将信用卡号作为HTTP请求发送给服务器。服务器则会将“生成订单&quot;的网页作为HTTP响应返回给客户端。</p> <p>不过，如果直接发送请求的话，信用卡号就很可能被窃听。下一节我们将探讨针对这种风险的对策。</p></blockquote> <div class="language- extra-class"><pre><code>                                                   **不使用SSL/TLS发送信用卡号的情形**
</code></pre></div><p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538754051163.png" alt="1538754051163"></p> <h3 id="_9-2-用ssl-tls承载http"><a href="#_9-2-用ssl-tls承载http" class="header-anchor">#</a> 9.2 用SSL/TLS承载HTTP</h3> <blockquote><p>什么是SSL,什么是TLS呢？官话说SSL是安全套接层(secure sockets layer)，TLS是SSL的继任者，叫传输层安全(transport layer security)。说白点，就是在明文的上层和TCP层之间加上一层加密，这样就保证上层信息传输的安全。如HTTP协议是明文传输，加上SSL层之后，就有了雅称HTTPS。它存在的唯一目的就是保证上层通讯安全的一套机制。</p> <p>当Web浏览器发送信用卡号时，信用卡号的数据会作为客户端请求发送给服务器。如果通信内容被窃听者Eve所窃取，Eve就会得到信用卡号。</p> <p>于是，我们可以用SSL（Secure Socket Layer）或者TLS（Transport Layer Security）作为对通信进行加密的协议，然后在此之上承載HTTP（下图）。通过将两种协议进行叠加，我们就可以对HTTP的通信（请求和响应）进行加密，从而防止窃听。通过SSL/TLS进行通信时，URL不是以http://开头，而是以https://开头。</p> <p>以上就是SSL/TLS的简单介绍。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538754381727.png" alt="1538754381727"></p> <blockquote><p>在大致了解了SSL/TLS之后，我们来整理一下SSL/TLS到底负责哪些工作。我们想要实现的是，通过本地的浏览器访问网络上的web服务器，并进行安全的通信。用上边的例子来说就是，Alice希望通过web浏览器向Bob书店发送信用卡号。在这里，我们有几个必须要解决的问题。</p> <ol><li>Alice的信用卡号和地址在发送到Bob书店的过程中不能被窃听。</li> <li>Alice的信用卡号和地址在发送到Bob书店的过程中不能被篡改。</li> <li>确认通信对方的Web服务器是真正的Bob书店。</li></ol> <p>在这里，（1）是机密性的问题；（2）是完整性的问题；而（3）则是认证的问题。</p> <p>要确保机密性，可以使用对称加密。由于对称加密算法的密钥不能被攻击者预测，因此我们使用伪随机数生成器来生成密钥。若要将对称加密的密钥发送给通信对象，可以使用非对称加密算法完成密钥交换。要识别篡改，对数据进行认证，可以使用消息认证码。消息认证码是使用单向散列函数来实现的。</p> <p>要对通信对象进行认证，可以使用对公钥加上数字签名所生成的证书。</p> <p>好，工具已经找齐了，下面只要用一个“框架”（framework）将这些工具组合起来就可以了。SSL/TIS协议其实就扮演了这样一种框架的角色。</p></blockquote> <p><strong>SSL/TLS也可以保护其他的协议</strong></p> <blockquote><p>刚才我们提到用SSL/TLS承载HTTP通信，这是因为HTTP是一种很常用的协议。其实SSL/TLS上面不仅可以承载HTTP，还可以承载其他很多协议。例如，发送邮件时使用的SMTP（Simple Mail Transfer Protocol, 简单邮件传输协议）和接收邮件时使用的POP3（Post Office Protocol，邮局协议）都可以用SSL/TLS进行承载。在这样的情况下，SSL/TLS就可以对收发的邮件进行保护。</p> <p>用SSL/TLS承载HTTP、SMTP和POP3的结构如下图所示。一般的电子邮件软件都可以完成发送和接收邮件这两种操作，其实是同时扮演了SMTP客户端和POP3客户端这两种角色。</p></blockquote> <p><img src="http://static.zhequtao.com/study/04mimaxue/day01changyong/1538755269669.png" alt="1538755269669"></p> <h3 id="_9-3-https"><a href="#_9-3-https" class="header-anchor">#</a> 9.3 https</h3> <h3 id="_9-3-1-http和https"><a href="#_9-3-1-http和https" class="header-anchor">#</a> 9.3.1 http和https</h3> <blockquote><p>HTTP协议：是互联网上应用最为广泛的一种网络协议，是一个客户端和服务器端请求和应答的标准（TCP），用于从WWW服务器传输超文本到本地浏览器的传输协议，它可以使浏览器更加高效，使网络传输减少。</p> <p>HTTPS：是以安全为目标的HTTP通道，简单讲是HTTP的安全版，即HTTP下加入SSL/TLS层，HTTPS的安全基础是SSL/TLS，因此加密的详细内容就需要SSL/TLS。</p> <p><font color="red">HTTPS协议的主要作用可以分为两种：</font></p> <ul><li><font color="red">建立一个信息安全通道，来保证数据传输的安全；</font></li> <li><font color="red">确认网站的真实性。</font></li></ul> <p>HTTPS和HTTP的区别主要如下：</p> <p>1、https协议需要到ca申请证书，一般免费证书较少，因而需要一定费用。</p> <p>2、http是超文本传输协议，信息是明文传输，https则是具有安全性的ssl/tls加密传输协议。</p> <p>3、http和https使用的是完全不同的连接方式，用的端口也不一样，前者是80，后者是443。</p> <p>4、http的连接很简单，是无状态的；HTTPS协议是由SSL/TLS+HTTP协议构建的可进行加密传输、</p> <p>身份认证的网络协议，比http协议安全。</p></blockquote> <h3 id="_9-3-2-https优缺点"><a href="#_9-3-2-https优缺点" class="header-anchor">#</a> 9.3.2 https优缺点</h3> <ul><li><p><strong>https的优点</strong></p> <blockquote><p>尽管HTTPS并非绝对安全，掌握根证书的机构、掌握加密算法的组织同样可以进行中间人形式的攻击，但HTTPS仍是现行架构下最安全的解决方案，主要有以下几个好处：</p> <ol><li>使用HTTPS协议可认证用户和服务器，确保数据发送到正确的客户机和服务器；</li> <li>HTTPS协议是由SSL+HTTP协议构建的可进行加密传输、身份认证的网络协议，要比http协议安全，可防止数据在传输过程中不被窃取、改变，确保数据的完整性。</li> <li>HTTPS是现行架构下最安全的解决方案，虽然不是绝对安全，但它大幅增加了中间人攻击的成本。</li> <li>谷歌曾在2014年8月份调整搜索引擎算法，并称 “比起同等HTTP网站，采用HTTPS加密的网站在搜索结果中的排名将会更高”。</li></ol></blockquote></li> <li><p><strong>https的缺点</strong></p> <blockquote><p>虽然说HTTPS有很大的优势，但其相对来说，还是存在不足之处的：</p> <ol><li>HTTPS协议握手阶段比较费时，会使页面的加载时间延长近50%，增加10%到20%的耗电；</li> <li>HTTPS连接缓存不如HTTP高效，会增加数据开销和功耗，甚至已有的安全措施也会因此而受到影响；</li> <li>SSL/TLS证书需要钱，功能越强大的证书费用越高，个人网站、小网站没有必要一般不会用。</li> <li>SSL/TLS证书通常需要绑定IP，不能在同一IP上绑定多个域名，IPv4资源不可能支撑这个消耗。</li> <li>HTTPS协议的加密范围也比较有限，在黑客攻击、拒绝服务攻击、服务器劫持等方面几乎起不到什么作用。最关键的，SSL证书的信用链体系并不安全，特别是在某些国家可以控制CA根证书的情况下，中间人攻击一样可行。</li></ol></blockquote></li></ul> <h2 id="参考资料"><a href="#参考资料" class="header-anchor">#</a> 参考资料</h2> <h3 id="pkcs15个标准"><a href="#pkcs15个标准" class="header-anchor">#</a> PKCS15个标准</h3> <blockquote><p>PKCS 全称是 Public-Key Cryptography Standards ，是由 RSA 实验室与其它安全系统开发商为促进公钥密码的发展而制订的一系列标准。</p> <p>可以到官网上看看 <a href="http://www.rsa.com/rsalabs/node.asp?id=2308" target="_blank" rel="noopener noreferrer">What is PKCS<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p> <p>http://www.rsa.com/rsalabs/node.asp?id=2308</p></blockquote> <p>PKCS 目前共发布过 15 个标准：</p> <ol><li><strong>PKCS#1：RSA加密标准。==PKCS#1定义了RSA公钥函数的基本格式标准，特别是数字签名==。它定义了数字签名如何计算，包括待签名数据和签名本身的格式；它也定义了PSA公/私钥的语法。</strong></li> <li>PKCS#2：涉及了RSA的消息摘要加密，这已被并入PKCS#1中。</li> <li>PKCS#3：Diffie-Hellman密钥协议标准。PKCS#3描述了一种实现Diffie- Hellman密钥协议的方法。</li> <li>PKCS#4：最初是规定RSA密钥语法的，现已经被包含进PKCS#1中。</li> <li>PKCS#5：基于口令的加密标准。PKCS#5描述了使用由口令生成的密钥来加密8位位组串并产生一个加密的8位位组串的方法。PKCS#5可以用于加密私钥，以便于密钥的安全传输（这在PKCS#8中描述）。</li> <li>PKCS#6：扩展证书语法标准。PKCS#6定义了提供附加实体信息的X.509证书属性扩展的语法（当PKCS#6第一次发布时，X.509还不支持扩展。这些扩展因此被包括在X.509中）。</li> <li>PKCS#7：密码消息语法标准。PKCS#7为使用密码算法的数据规定了通用语法，比如数字签名和数字信封。PKCS#7提供了许多格式选项，包括未加密或签名的格式化消息、已封装（加密）消息、已签名消息和既经过签名又经过加密的消息。</li> <li>PKCS#8：私钥信息语法标准。PKCS#8定义了私钥信息语法和加密私钥语法，其中私钥加密使用了PKCS#5标准。</li> <li>PKCS#9：可选属性类型。PKCS#9定义了PKCS#6扩展证书、PKCS#7数字签名消息、PKCS#8私钥信息和PKCS#10证书签名请求中要用到的可选属性类型。已定义的证书属性包括E-mail地址、无格式姓名、内容类型、消息摘要、签名时间、签名副本（counter signature）、质询口令字和扩展证书属性。</li> <li>PKCS#10：证书请求语法标准。PKCS#10定义了证书请求的语法。证书请求包含了一个唯一识别名、公钥和可选的一组属性，它们一起被请求证书的实体签名（证书管理协议中的PKIX证书请求消息就是一个PKCS#10）。</li> <li>PKCS#11：密码令牌接口标准。PKCS#11或“Cryptoki”为拥有密码信息（如加密密钥和证书）和执行密码学函数的单用户设备定义了一个应用程序接口（API）。智能卡就是实现Cryptoki的典型设备。注意：Cryptoki定义了密码函数接口，但并未指明设备具体如何实现这些函数。而且Cryptoki只说明了密码接口，并未定义对设备来说可能有用的其他接口，如访问设备的文件系统接口。</li> <li>PKCS#12：个人信息交换语法标准。PKCS#12定义了个人身份信息（包括私钥、证书、各种秘密和扩展字段）的格式。PKCS#12有助于传输证书及对应的私钥，于是用户可以在不同设备间移动他们的个人身份信息。</li> <li>PDCS#13：椭圆曲线密码标准。PKCS#13标准当前正在完善之中。它包括椭圆曲线参数的生成和验证、密钥生成和验证、数字签名和公钥加密，还有密钥协定，以及参数、密钥和方案标识的ASN.1语法。</li> <li>PKCS#14：伪随机数产生标准。PKCS#14标准当前正在完善之中。为什么随机数生成也需要建立自己的标准呢？PKI中用到的许多基本的密码学函数，如密钥生成和Diffie-Hellman共享密钥协商，都需要使用随机数。然而，如果“随机数”不是随机的，而是取自一个可预测的取值集合，那么密码学函数就不再是绝对安全了，因为它的取值被限于一个缩小了的值域中。因此，安全伪随机数的生成对于PKI的安全极为关键。</li> <li>**PKCS#15：密码令牌信息语法标准。**PKCS#15通过定义令牌上存储的密码对象的通用格式来增进密码令牌的互操作性。在实现PKCS#15的设备上存储的数据对于使用该设备的所有应用程序来说都是一样的，尽管实际上在内部实现时可能所用的格式不同。PKCS#15的实现扮演了翻译家的角色，它在卡的内部格式与应用程序支持的数据格式间进行转换。</li></ol></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">11/22/2020, 4:37:35 AM</span></div></footer> <div class="page-nav"><p class="inner"><!----> <span class="next"><a href="/go-block/blockchain/encryption.html">密码学安全</a>
      →
    </span></p></div> </main></div><div class="global-ui"></div></div>
    <script src="/assets/js/app.cb35c8f6.js" defer></script><script src="/assets/js/2.063846a6.js" defer></script><script src="/assets/js/4.1ffb4609.js" defer></script><script src="/assets/js/45.f8a2a3b3.js" defer></script>
  </body>
</html>
